The use of Python functions is described in detail in this paper, and it is of great value for beginners to learn Python's friends. Share it for everyone's reference. The specific analysis is as follows:
In general, the function of Python is written by a new statement that DEF,DEF is an executable statement--the function does not exist until Python has run def.
Functions are passed by assignment, and parameters are passed to the function by assignment
The DEF statement creates a function object and assigns it to a variable name, and the general format of the DEF statement is as follows:
def
(arg1,arg2,arg3,......,argn):
The DEF statement executes in real time, and when it runs, it creates and assigns a new function object to a variable name, and all of the Python statements are executed in real time, without a process like a separate compile time
Because it is a statement, Def can appear where any statement can occur-even nested in other statements:
if test: def fun (): ... else: def func (): ... func ()
You can assign a function to a different variable name and call it with a new variable name:
Othername=func () Othername ()
Create a function
The built-in callable function can be used to determine whether a function can be called:
>>> Import math>>> x=1>>> y=math.sqrt>>> callable (x) false>>> callable (y ) True
Use the DEL statement to define the function:
>>> def Hello (name): Return ' hello ', ' +name+ '! ' >>> Print Hello (' world ') Hello, world!>>> print hello (' Gumby ') Hello, gumby!
Write a fibnacci sequence function:
>>> def fibs (num): result=[0,1] for i in range (num-2): result.append (result[-2]+result[-1])
return result>>> fibs [0, 1, 1, 2, 3, 5, 8,,, 34]>>> fibs (15) [0, 1, 1, 2, 3, 5, 8, 13, 21 , 34, 55, 89, 144, 233, 377]
Assigning a value to a parameter within a function does not change the value of any external variable:
>>> def try_to_change (n): n= ' Mr.gumby ' >>> name= ' mrs.entity ' >>> try_to_change (name) > >> name ' Mrs. Entity '
Because strings (and tuples and numbers) are immutable, they do not change when parameters are made, but what happens if you use a mutable data structure such as a list as a parameter:
>>> name= ' mrs.entity ' >>> try_to_change (name) >>> name ' Mrs. Entity ' >>> def Change (n): n[0]= ' Mr.gumby ' >>> name=[' mrs.entity ', ' mrs.thing ']>>> Change (name) >>> name[' Mr.gumby ', ' mrs.thing ']
Parameters have changed, which is the important difference from the previous example
The following do not use the function again:
>>> name=[' mrs.entity ', ' mrs.thing ']>>> n=name #再来一次, analogue pass behavior >>> n[0]= ' Mr.gumby ' #改变列表 > >> name[' Mr.gumby ', ' mrs.thing ']
When 2 variables refer to a list at the same time, they do refer to a list at the same time, to avoid this, you can copy a copy of a list, and when you slice in a sequence, the slice returned is always a copy, so you copy the entire list of slices, and you get a copy:
>>> names=[' mrs.entity ', ' mrs.thing ']>>> n=names[:]>>> n is namesfalse>>> n== Namestrue
Changing n At this time does not affect the names:
>>> n[0]= ' Mr.gumby ' >>> n[' Mr.gumby ', ' mrs.thing ']>>> names[' mrs.entity ', ' mrs.thing ' >>> change (names[:]) >>> names[' mrs.entity ', ' mrs.thing ']
Keyword parameters and default values
The order of the parameters can be given by giving the parameter a name (but the parameter name and value must correspond):
>>> def hello (greeting, name): print '%s,%s! '% ( Greeting, name) >>> hello (greeting= ' hello ', name= ' world! ') hello,world!!
The most powerful thing about keyword parameters is that you can provide default values for parameters in Parameters:
>>> def hello_1 (greeting= ' Hello ', name= ' world! '): print '%s,%s! '% ( Greeting,name) >>> hello_1 () hello,world!! >>> hello_1 (' Greetings ') greetings,world!! >>> hello_1 (' greeting ', ' universe ') greeting,universe!
If you want greeting to use the default value:
>>> hello_1 (name= ' Gumby ') hello,gumby!
You can provide any number of parameters to the function, and it is not difficult to implement them:
>>> def print_params (*params): print params>>> print_params (' testing ') (' testing ',) >> > Print_params (1, 2, 3)
Mix common parameters:
>>> def print_params_2 (title,*params): print title print params>>> print_params_2 (' params : ', A-print_params_2 ' params: (1, 2, 3) >>> (' Nothing: ') to Nothing: ()
The asterisk means "collect the rest of the positional parameters", and if you do not provide any elements for collection, the params is an empty tuple
However, you cannot handle keyword parameters:
>>> print_params_2 (' Hmm ... ', something=42) Traceback (most recent call last): File "
", line 1, in< c5/>
print_params_2 (' Hmm ... ', something=42) typeerror:print_params_2 () got an unexpected keyword argument ' Something '
Try using "* *":
>>> def print_params (**params): print params>>> print_params (x=1,y=2,z=3) {' Y ': 2, ' X ': 1, ' Z ': 3} >>> def parames (x,y,z=3,*pospar,**keypar): print x, y, z print pospar print keypar>>> Parames (1,2,3,5,6,7,foo=1,bar=2) 1 2 3 (5, 6, 7) {' Foo ': 1, ' bar ': 2}>>> parames () 1 2 3 () {}>>> def prin T_params_3 (**params): print params>>> Print_params_3 (x=1,y=2,z=3) {' Y ': 2, ' X ': 1, ' z ': 3}>>> #返回 is a dictionary instead of tuples >>> #组合 ' # ' with ' # # ' >>> def print_params_4 (x,y,z=3,*pospar,**keypar): print x, y, z Print Pospar print keypar>>> print_params_4 (1,2,3,5,6,7,foo=1,bar=2) 1 2 3 (5, 6, 7) {' Foo ': 1, ' Bar ': 2}> >> Print_params_4 (1) 2 3 () {}
It is believed that this article has some reference value to the study of Python program design.