table of Contents
1. vararg
1.1 Name of the parameter *
def f1(*args): # 调用函数时,有多少个位置实参,我就接收多少个参数
print(args)
f1() # () # a是空元组
# f1(1) # (1, )
# f1(1, 2)
f1(1, 2, 3, 3, 4, 5, 5, 6, 7, 7, 7, 7, 7, 7, 7, )
# *args(约定俗称的),用元组接收 多余 的位置实参
The parameter names 1.2 **
def f1(**kwargs):
print(kwargs)## f1(x=1) # 空字典
# **kwargs,用字典接收多余的关键字实参
1.3 Name of the argument * (understand)
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[0],lt[1],lt[2],lt[3],lt[4],lt[5],lt[6])
f1(*lt) # *lt把列表中的元素打散成位置实参依次传给位置形参
1.4 the name of the argument ** (understand)
def f1(z, b):
print(z, b)
dic = {'z': 1, 'b': 2} # a=1,b=2
f1(**dic) # **dic把字典打散成关键字实参然后传给函数f1
2. function object
Everything in python objects
2.1 references
func = f1
print('f1:', f1)
print('func:', func)
func()
2.2 container element
lt = [f1, 1, 2, 3]
print('lt[0]', lt[0])
print('f1', f1)
lt[0]()
2.3 as a function argument
def f2(f2_f1):
print('f2_f1',f2_f1)
f2_f1()
f2(f1)
print('f1', f1)
2.4 as a function return value
def f2(f2_f1):
return f2_f1
res = f2(f1)
print('res', res)
print('f1', f1)
res()
Reference (copy)
As a container class elements
As a function of the argument
As the return value of the function
Contact function object
def register():
print('register')
def login():
print('login')
def withdraw():
print('wightdraw')
def shopping():
print('shopping')
func_dict = {
'1': register,
'2': login,
'3': withdraw,
'4': shopping,
}
print('''
1 注册
2 登录
3 提现
4 购物
''')
while True:
choice = input('请选择你需要的功能(输入q退出):')
if choice == 'q':
break
func_dict[choice]()
3. nested function
Print multiplication table
for i in range(1, 10): # i控制的是行
for j in range(i): # j控制的是列
print(f'{j+1}*{i}={(i)*(j+1)}', end=' ')
print() # 打印换行
'''
i控制行,j控制列
i = 1 for j in range(1) j = 0 + 1
i = 2 for j in range(2) j = 0 + 1 j = 1 + 1
i = 3 for j in range(3) j = 0 + 1 j = 1 +1 j = 2 + 1
i = 4
i = 5
Nested functions: a function which has the function
# 定义函数,只检测语法,不会执行代码
def f1():
print('from f1')
def f2():
print('from f2')
res = f1()
Internal function defined functions, can not be used outside
4. Name of space and scope
4.1 Variable names / function names - "Name -" namespace: special storage name
4.2 built-in namespace: stores the name of the built-in method
- 3 Data type method comes with built-in; Python interpreter own built-in method (print / len / list / str / dict)
Global name space: In addition to the built-in global and local call
def f1():
x = 10
def f2():
z = 20
y = 10
lt = [1, 2, 3]
The local name space: a local call within the function definitions
4.1 namespace execution (generation) order:
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 with your current location to find, and then looking for can not be found locally in this order, not against the direction - "Global -" Built - "error
len =10
def f1():
# len = 20
print(len)
f1()
def f1():
print('from f1')
def f2():
print('from f2')
res = f1()
4.2 Scope
Global scope: built-in namespace + global name space - "global scope
def f1():
y = 2
print(1, y)
y = 10
f1()
print(2, y)
print(y)
Local scope
def f1():
def f2():
def f3():
x = 1
print(x)
x = 2
f3()
f2()
f1()
The local scope: Local namespace - 'local name space
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
global and nonlocal understand, try not to use
global: breaking the rules described above in point 1
x = 10
def f1():
global x # 让global以下的局部的x变成全局的x
x = 20
f1()
print(x)
nonlocal: breaking the rules above Point 2
x = 10
def f1():
def f2():
def f3():
nonlocal x # nonlocal让x成为顶层函数的局部,不是让他成为全局 # 压根没有应用情景,尽量不要嵌套函数
x = 1
x = 2
f3()
print(1, x)
f2()
def f0():
# x = 3 # f1的nonlocal无法修改
# f1()
f1()
print(2, x)
All variable data types can break all the rules above
lt = [10]
def f1():
lt.append(12)
lt[0] = 11
f1()
print(lt)python
Remember you do not understand / forget
def f1(i, lt=[]):
lt.append(i)
print(lt)
for i in range(10):
f1(i)
def f1(i, lt):
lt.append(i)
print(lt)
for i in range(10):
f1(i, [])
Defined functions: detect only the syntax, the code is not performed
def f1():
def f2():
def f3():
print('f3')
f3()
f1()
x = 10
print(x)
def f1():
x = 10
print(x)
f1()