6.1 laziness is a virtue
Currently, the programs we write are very small. If you want to write a large program, you need to reuse the code. This problem can be solved by creating a function.
6.2 abstraction and Structure
Abstraction can save a lot of work, and modular programs are easier to understand, as other programming languages do.
6.3 create a function
A function can be called. It executes an action and returns a value (Of course, not all functions return values). Generally, the built-in callable function can be used to determine whether the function is callable.
>>> import math>>> x=1>>> y=math.sqrt>>> callable(x)False>>> callable(y)True
Use def statements to define functions
>>> def hello(name):return 'hello,'+name+'!'>>> hello ("daxiao")'hello,daxiao!'
6.3.1 record Function
If you want to write a document to the function to make it understandable to those who use the function later, you can add comments (starting ). Another way is to write strings directly. If a string is written at the beginning of a function, it is stored as part of the function, which is called a document string.
>>> def hello(name):'name'return 'hello'+name+'!'>>> hello.__doc__'name'
You can also use help to access
>>> help(hello)Help on function hello in module __main__:hello(name) name
6.3.2 functions that are not real functions
Actually, it is equivalent to the void type we used previously. Some operations can be performed, and the print result is None.
>>> def test():print 'this is morning '>>> x=test()this is morning >>> x>>> print xNone
6.4 parameter magic
A brief review of the basics
6.4.1
6.4.2 can I change the parameters?
The function obtains a series of values through its parameters. The parameters are only variables:
>>> def trytochange(n):n='l'>>> name='x'>>> trytochange(name)>>> name'x'
Just like the previous C and hava parameters, the real parameters are passed in and then changed. Of course, the value of the real parameters will not change.
But what if the parameter is a variable structure, such as a list?
>>> def chang(n):n[0]='l'>>> name('x','l')Traceback (most recent call last): File "
", line 1, in
name('x','l')TypeError: 'str' object is not callable>>> name=['x','l']>>> chang(name)>>> name['l', 'l']
Just like in C, when the form parameter is an array, the real parameter also changes by changing the form parameter. To prevent this situation, remember to copy the parameter first When referencing.
When parameters are unchangeable, how can we modify parameters through functions?
>>> def inc(x):return x+1>>> x=2>>> x=inc(x)>>> x3
6.4.3 keyword parameters and default values
Currently, all the parameters we use are called location parameters because their location is very important-in fact, they are more important than their names. this function introduced in this section can avoid location issues. When you gradually get used to this function, you will find that the larger the program size, the greater their role.
Sometimes, parameters are hard to remember. To make things easier, you can provide the parameter name.
>>> def hello_1(greeting ,name):print '%s,%s'%(greeting,name)>>> hello_1(name='world',greeting='hello')hello,world
In this way, the location is completely unaffected.
The most powerful keyword parameter is that the default value can be provided to the parameter in the function. When the parameter has a default value, it can be called or provided only a part of the parameter.
>>> def hello_1(greeting='hello' ,name='world'):print '%s,%s'%(greeting,name)>>> hello_1(name='liang')hello,liang
6.4.4 collection Parameters
If a user can provide multiple parameters for a function, it is not difficult to implement it.
>>> def print_(*p):print p>>> print_(1,2,3)(1, 2, 3)