Vararg
Vararg: refers to a function call, the number of parameters passed may not be fixed
* Parameter (parameter name)
* Args (convention), receiving extra argument position, receiving tuple
def f1(*a) # 调用函数时,有多少个参数,我就接收多少个参数
print(a)
f1() # () # a是空元组
f1(1) # (1,)
f1(1,2)
f1(1,2,3,4,5,6,7,6,6,)
f1(b=1) #报错
def f1(*abc,b):
print(abc,b)
f1(b=1) # () 1
** Katachisan
** kwargs, receiving extra keyword arguments dictionary
def f1(**kwargs):
print(kwargs)
f1() # 空字典
f1(x=1) # {'x':1}
def f1(*args,**kwargs):
print('args:',args)
print('kwargs:'kwagrs)
f1(11,1,12,2,x=1,y=2,z=3)
# agrgs:(11,1,12,2)
# kwargs:('x':1,'y':2,'z':3)
def f1(a,b,c,*args,**kwargs):
print('args:',args)
print('kwargs:'kwagrs)
f1(11,1,12,2,x=1,y=2,z=3)
# agrgs:(2,)
# kwargs:('x':1,'y':2,'z':3)
Remember that is receiving extra value
*Arguments
def f1(a,b,c,d,e,f,g):
print(a,b,c,d,e,f,g)
lt = [1,2,3,4,5,6,7]
#f1(lt[0],t[1],t[2],t[3],t[4],t[5],t[6]) 换成 f1(*lt)
f1(*lt) # *lt把列表中的元素打散成位置实参一次传给位置形参
# 1 2 3 4 5 6 7
**Arguments
def f1(*args,**kwargs):
print('args',args)
print('kwargs',kwargs)
# def f1(x,y): 报错
dic = {'a':1,'b':2} # a=1,b=2
f1(**dic) # **dic把字典打散成关键字实参然后传给函数f1
# args()
# kwargs{'a':1,'b':2}
Function object
Everything in python objects
s = 's1'
lt = ['skdjf']
Functions are first-class objects, namely data processing functions can be used as
def func():
print()
print(func)
<function func at 0x10af72f28>
First, the four functions function object
- Quote
x = 'hello nick'
y = x
f = func
print(f)
<function func at 0x10af72f28>
- As an argument to a function
len(x)
def foo(m)
m()
foo(func)
from func
- There are as function return values
def foo(x):
return x
res = foo(func)
print(res)
res()
- It can be used as container type elements
l = [x]
function_list = [func]
function_list[0]()
from func
Exercise function object
def pay():
print('支付1e成功')
def withdraw():
print('提现2e成功')
dic = {
'1':pay,
'2':withdraw,
}
while True:
msg = '''
'1':支付,
'2':提现,
'3':退出,
'''
print(msg)
choice = input('选择功能:').strip()
if choice == '3':
break
elif choice in dic :
dic[choice]()
Nested functions
Nested function definition
Internal function defined functions can not be used inside a function defined in an external function.
def f1():
def f2():
print('from f2')
f2()
f2() # NameError: name 'f2' is not defined
def f1():
def f2():
print('from f2')
f2()
f1()
from f2
Nested function calls
def max2(x,y):
if x > y:
return x
else:
return y
def max4(a,b,c,d):
res1 = max2(a,b)
res2 = max2(c,d)
res = max2(res1,res2)
return res
print(max4(1,2,3,4))
4
Namespace and scope
First, the name space
Namespace (name spaces): create a variable is actually opened up a new space in memory.
The memory space binding relationship between a variable name and variable stored in memory, and this space is called a namespace .
- Built-in namespace
Built-in namespace: Store Pyhton interpreter that comes with the name, such asint、float、len
Life cycle: when the interpreter started to take effect, fail when the interpreter is closed
- Global name space
Global name space: In addition to internal and local names, the rest are stored in the global name space.
The following codex、func、l、z
Life cycle: when the file is executed into effect, expire after file execution
x = 1
def func():
pass
l = [1, 2]
if 3 > 2:
if 4 > 3:
z = 3
- The local name space
The local name space: the name of the function body for storing generated during a function call, such as the following codef2
Life Cycle: take effect during the function call when the file is executed, expire after the function execution
def f1():
def f2():
print('from f2')
f2()
f1()
Loading order
Built-in - "Global -" local
Find the order
Current - "Global -" Built
x = 1
y = 2
len = 100
def func():
y = 3
len = 1000
print(f"y: {y}")
print(f"len: {len}")
# print(a) # NameError: name 'a' is not defined
func()
s: 3
only: 1000
Second, the scope
Domain refers to a region, i.e. the region scope of action.
- Global scope
Global Scope: Global effective, global survival, and includes built-in namespace global name space.
# 全局作用域
x = 1
def bar():
print(x)
bar()
- Local scope
The local scope: partially valid, temporary storage, containing only the local name space.
# 局部作用域
def f1():
def f2():
def f3():
print(x)
x = 2
f3()
f2()
f1()
- important point
Note that: the scope of the relationship between the function definition stage fixed die, regardless of the function was called.
# 作用域注意点
x = 1
def f1(): # 定义阶段x=1
print(x)
def f2():
x = 2
f1()
f2()
1
- global keyword
Modify variables in global scope.
x = 1
def f1():
x = 2
def f2():
# global x # 修改全局 输出 3
x = 3
f2()
f1()
print(x)
1
- nonlocal keyword
Modify variables local scope.
x = 1
def f1():
x = 2
def f2():
# nonlocal x 输出 3
x = 3
f2()
print(x)
f1()