6.4.5 reversal process
We have already discussed how to collect parameters as tuples and dictionaries. But in fact, if you use * and *, you can perform the opposite operation.
>>> def add(x,y): return x+y>>> params=(1,2)>>> add(*params)3
You can use the same technology to process dictionaries.
>>> def hello_3(greeting ='hello',name='world'):print '%s, %s'%(greeting,name)>>> params={'name':'daxiao','greeting':'i love'}>>> hello_3(**params)i love, daxiao
6.5 Scope
Like C, variables defined within a function are valid only within the function.
Read global variables:
>>> def combine(temp):print temp+temp2>>> temp2='b'>>> combine('a')ab
Change global variables
>>> x=1>>> def change():global xx+=1>>> change()>>> x2
6.6 Recursion
6.6.1 example
Fibonacci series:
ef f(i):if(i==1):return 1else:if(i==2):return 1else:return f(i-1)+f(i-2)
6.6.2 Binary Search
Omitted.
This chapter is relatively simple in general. After all, it is all the content you have learned before.
Summary
Abstraction
Function Definition
Parameters
Scope
Recursion
Function Programming