table of Contents
A variable length parameter
* Katachisan
The function is called, the receiving position of excess tuple argument
def f1 (* args): args to do with the general convention parameter variable name
Print (args)
** Katachisan
When you call the function, receiving extra keyword arguments dictionary
def f1 (** kwargs): # convention generally do with kwargs * parameter variable name
print (kwargs)
*Arguments
When the parameter passing, the elements in the list and then broken into position successively passed argument position parameter
def f1(a, b, c, e, d, f, g):
print(a, b, c, e, d, f, g)
lt = [1, 2, 3, 4, 5, 6, 7]
f1(*lt)
**Arguments
When the parameter passing, the dictionary elements broken into Keyword argument followed position parameter passed
def f1(z, b):
print(z, b)
dic = {'z': 1, 'b': 2} # a=1,b=2
f1(**dic)
Second, the function object
Everything in python objects
It is also a function of the object
Function object function name =
+ Function name () that is called, is calling
Reference (copy)
def f1():
print('from f1')
func = f1
print('f1:', f1)
print('func:', func)
func()
Container element
def f1():
print('from f1')
lt = [f1, 1, 2, 3]
print('lt[0]', lt[0])
print('f1', f1)
lt[0]()
As the argument of function
def f1():
print('from f1')
def f2(f2_f1):
print('f2_f1',f2_f1)
f2_f1()
f2(f1)
As the return value of the function
def f1():
print('from f1')
def f2(f2_f1):
return f2_f1
res = f2(f1) # 即res = f1
print('res', res)
print('f1', f1)
res()
Third, nested functions
Nested functions: a function which has the function
Defining a function, only detect syntax, the code will not be executed
Internal function defined functions, can not be used outside
Fourth, namespace and scope
Namespace executed (generated) sequence
- Built-in Built-in namespace: python interpreter starts when there is a
- Global name space: time code executable file will have a global
- The local name space: time function call will have a local
Search Order
Start looking for the current location, and then can not find in that order, not against the direction of looking
Local - "Global -" Built - "error
Namespaces
Dedicated memory used to store the name space
Built-in namespace
Storage space in the name of the built-in method
Global name space
In addition to the built-in global and local call
The local name space
Local call within the function definitions
Scope
Global scope
Built-in namespace = + global namespace global scope
Local scope
Local local scope namespace =
Features local scope
- Global scope and local scope x x relationship is not half dime
- Local scope x 1 x 2 and the local scope is also no relationship, then the two even with a local scope, and a local scope local scope