1. Define the function:
In Python, you define a function to use a def statement, write out the function name, parentheses, the arguments in parentheses, and a colon, and : then, in the indent block, write the function body, and the return value of the function is returned with a return statement. If a function does nothing, it needs to use the PASS statement (C,java can directly not write the code, Python must write the pass placeholder, or error).
def my_abs (x): if x >= 0: return x else: return-x
2. Parameter check:
Isinstance (object, ClassInfo), whose first argument is an object, and the second is a list of type names or type names. Its return value is a Boolean type. Returns True if the type of the object is the same as the type of parameter two . If parameter two is a tuple, returns True if the object type is the same as one of the type names in the tuple .
def my_abs (x): If not isinstance (x, (int., float)): raise TypeError (' bad operand type ') if x >= 0: return x else: return-x
3. Return multiple values:
The python parameter can return multiple values, essentially returning a tuple. Multiple variables can receive one tuple at a time, assigning values by location.
4. About Parameters:
4.1 Default parameters:
Python's function parameters can be set to a default value, which is handled by default when the corresponding parameter is not passed in. As the following example prints, it is 25.
def power (x, n=2): s = 1 while n > 0: n = n-1 s = s * x return sprint power (5) Print power (5, 2)
When there are multiple default parameters, they can be passed in order or by using parameter name = parameter value.
def enroll (name, Gender, age=6, city= ' Beijing '): print ' name: ', name print ' Gender: ', gender print ' Age: ', Age print ' city: ', city
Enroll (' Adam ', ' M ', city = ' Chengdu ')
Enroll (' Tom ', ' M ', 7)
The default parameter values are generally immutable objects, otherwise the default parameter values may change with the call. For example:
def add_end (l=[]): l.append (' End ') return L
Print Add_end (),
Print Add_end ()
Output is[‘END‘] [‘END‘, ‘END‘]。
4.2 Variable parameters:
A variable parameter is a variable number of parameters passed in, but can also be implemented by a list or tuple. The mutable parameters are as follows:
Def calc (*numbers): sum = 0 for n in numbers: sum = SUM + N * n return sum
print calc(1, 2, 3)
print calc()
If the parameter you want to pass in is stored in the list, you can calc (*listname).
4.3 Keyword parameters:
The keyword parameter allows you to pass in 0 or any parameter with parameter names that are automatically assembled inside the function as a dict. Keyword arguments preceded by * *, examples are as follows:
def person (name, age, **kw): print ' name: ', Name, ' Age: ', age, ' other: ', kw
person(‘Bob‘, 35, city=‘Beijing‘)
kw = {‘city‘: ‘Beijing‘, ‘job‘: ‘Engineer‘}print person(‘Jack‘, 24, **kw)
4.4 parameter combinations:
4 parameters can be used together, or only some of them, but note that the order of parameter definitions must be: required, default, variable, and keyword parameters.
def func (A, B, c=0, *args, **kw): print ' A = ', A, ' B = ', B, ' C = ', C, ' args = ', args, ' kw = ', kw
args = (1, 2, 3, 4)kw = {‘x‘: 99}print func(*args, **kw) #对于任意函数,都可以通过func(*args, **kw)的形式调用它# 打印结果:a = 1 b = 2 c = 3 args = (4,) kw = {‘x‘: 99}
Python Learning notes--functions