First, the definition of the function:
Python uses the DEF keyword to define a function, which includes the function name and parameters, does not need to define a return type, and Python can return any type:
Python code
- #没有返回值的函数, it actually returns none.
- def run (name):
- print name,' runing ' #函数体语句从下一行开始, and the first line must be indented
- >>>run (' xiaoming ')
- Xiaoming runing
- >>>print run (' xiaoming ')
- Xiaoming runing
- None #如果没有ruturn语句, the function returns none
- #有返回值的参数
- def run (name):
- return name+' runing '
- >>>r = run (' xiaoming ')
- >>>r
- Xiaoming runing
Second, the document string:
In Python, comments are represented by #, and no multiline comments are found yet. However, you can use a multiline string to write inside a function:
Python code
- def run (name):
- "" " Print Somebody Runing" ""#写在函数体的第一行才叫文档字符串
- print name,' runing '
You can use __doc__ to view the contents of a function's document string
Java code
- >>>run.__doc__
- Print Somebody runing
Three, Parameters:
The parameter list of a Python function can be any number, call the function, take the position binding and the keyword binding two ways, confirm the parameters of the passed variables!
The code shown above can be considered a positional binding .
Here's a look at the keyword bindings :
Python code
- def run (name,age,sex):
- print ' name: ', name,' Age: ', age,' sex: ', sex
- >>> Run (age=23,name=' xiaoming ', sex=' boy ')#关键字绑定
- Name:xiaoming Age:sex:boy
A parameter cannot use the position and keyword bindings at the same time in a single call
Python code
- def run (name,age,sex):
- print ' name: ', name,' Age: ', age,' sex: ', sex
- >>> run (' xiaoming ', name=' xiaoming ', sex=' boy ')
- Syntaxerror:non-keyword arg after keyword arg
when a function is called, if the first argument uses a keyword binding, the following argument must also use the keyword binding!
Default Parameters :
Python code
- def run (name,age=20,sex=' girl '):
- Print Name,age,sex
- >>>run (' nana ')
- Nana Girl
- >>>run (' nana ', +)
- Nana Girl
- >>>run (' GG ',' boy ')#使用的位置绑定, so python binds ' boy ' to age, not the sex we want
- GG Boy Girl
- >>>run (' GG ', sex=' boy ')#混合关键字绑定, can achieve the desired effect
- GG Boy
1, If the parameters of a function contains default parameters, then all parameters after this default parameter must be the default parameters ,
otherwise it throws: Syntaxerror:non-default argument follows default argument exception.
Python code
- def run (name,age=10,sex):
- print name, age, sex
- Syntaxerror:non-default argument follows default argument GG
Several exceptions
Python code
- def run (name,age,sex=' boy '):
- Print Name,age,sex
- >>>run ()#required argument missing
- >>>run (name=' GG ',23°c)#non-keyword argument following keyword
- >>>run (' GG ', name=' pp ')#duplicate value for argument
- >>>run (actor=' xxxx ')#unknown keyword
- #第一种情况是丢失参数
- #第二种情况是: If the first keyword binding is used, the following must use the keyword binding
- #第三种情况: You cannot use both location and keyword bindings in a single call
- #第四种情况: You cannot use a keyword outside the argument list
2. The default parameters are parsed in the function definition segment and resolved only once .
Python code
- >>>i = 5
- >>>def f (arg=i):
- >>> Print arg
- >>>i = 6
- >>>f ()
- 5 #结果是5
When the default value is a Mutable object, such as a linked list, a dictionary, or most class instances, there are some differences:
Python code
- >>> def F (A, l=[]):
- >>> L.append (a)
- >>> return L
- >>> Print F (1)
- >>> print F (2)
- >>> print F (3)
- [1]
- [1, 2]
- [1, 2, 3]
- #可以用另外一种方式实现:
- >>> def F (A, l=None):
- >>> If L is None:
- >>> L = []
- >>> L.append (a)
- >>> return L
Variable parameters
Parameters are wrapped into a tuple. Before these variable number of parameters, there can be 0 to more common parameters:
Python code
- def run (Name,*args):
- print name,' runing '
- For A in args: print a
- >>> run (' GG ',' mm ')
- GG runing
- Mm
- >>> run (' GG ',1,2,' mm ')
- GG runing
- 1
- 2
- Mm
- >>> run (' GG ',1,1.02,[' mm ',' GM ')
- GG runing
- 1
- 1.02
- [' mm ',' GM ']
Visible variable parameters can be any number, and any type (and can be mixed)
variable parameters for keyword bindings (**args this form, look at the original document, not very understanding, for the moment called)
Python code
- def run (Name,**args):
- Keys = Args.keys ()
- For K in keys:
- Print K,args[k]
- >>> run (' Nana ', type=' open ')
- Type Open
- >>> run (' Nana ', type=' open ', title=' Gogo ')
- Type Open
- Title Gogo
- #*arg must be in front of **args.
- def run (Name,*arg,**args):
- For A in arg:print a
- Keys = Args.keys ()
- For K in keys:
- Print K,args[k]
- >>> run (' nn ',' mm ',1,2,' oo ', type=' open ', title=' Gogo ')
- Mm
- 1
- 2
- Oo
- Type Open
- Title Gogo
Splitting of parameter columns
Python code
- >>> range (3, 6) # normal call with separate arguments
- [ 3, 4, 5]  
- >>> args = [3, 6]
- >>> range (*args) # call with arguments unpacked from a list
- [3, 4, 5]  
By using the lambda keyword, you can create short, anonymous functions
Python code
- #这个例子没怎么看懂
- >>> def make_incrementor (n):
- ... return Lambda x:x + n #相当于创建了一个一x为参数的匿名函数?
- ...
- >>> f = make_incrementor#f = Make_incrementor (n=42), sets the value of n
- >>> f (0)#其实调用的是匿名函数?
- 42
- >>> f (1)
- 43
- #看下面一个例子报的错误明白一点了
- >>>def t (n):
- ... Print X*n
- >>>m = t (2)
- Traceback (most recent):
- File "<pyshell#85>", line 1, in <module>
- m = t (2)
- File "<pyshell#84>", line 2, in t
- Print X*n
- Nameerror: Global name ' x ' is not defined
- Says no global name ' x ' is defined
- >>> x =Ten
- >>> def t (n):
- ... Print X*n
- >>> m = t (2)
- 20