Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - How to set the type of function parameters in python
How to set the type of function parameters in python
#Python 2.5

# This can be done by interior decoration.

# However, the parameter types are generally not limited.

# Give you an idea:

Def arg filter(* type):

Def deco (function):

# This is a modifier

def newfunc(*args):

# New features

if len(types)==len(args):

Correct = correct

For I(len(args)) in the range:

If it is not isinstance(args[i], types[i]):

# Judge the type

Right = wrong

If correct:

Return function (* parameter)

# Returns the original function value

Otherwise:

Wrong lifting type

Otherwise:

Wrong lifting type

Return to newfunc

# The decorator returned a new function.

Huigui decoration

# Returns a decorative function

@argfilter(int,str)

# Specify the parameter type

Define the function (i, s):

# Define a modifier function

Print id card

# After that, if you want to limit the types, that's it:

#@argfilter (the class name of the first parameter, the class name of the second parameter, ... the class name of the nth parameter)

#def yourfunc (first parameter, first parameter, ... nth parameter):

#

...

#

# Equivalent to:

#def yourfunc (first parameter, first parameter, ... nth parameter):

#

...

#yourfunc = argfilter (class name of the first parameter, class name of the second parameter, ..., class name of the nth parameter) (yourfunc)