一:复习巩固题
1、以下格式自行推导至少10遍
def index(x, y):
print(x, y)
def wrapper(*args, **kwargs): # args=(1,2) kwargs=
index(*args, **kwargs) # index(1,2,a=1,b=2)
# index(*(1,2),**{"a":1,"b":2}) # index(1,2,a=1,b=2)
wrapper(1, 2, a=1, b=2)
def index(x,y):
print(x,y)
def wrapper(*args,**kwargs):#args=(1,2) kwargs=
index(*args,**kwargs)#index(1,2,a=1,b=2)
# index(*(1,2),**{"a":1,"b":2}) # index(1,2,a=1,b=2)
wrapper(1,2,a=1,b=2)
def index(x,y):
print(x,y)
def wrapper(*args,**kwargs):#args=(1,5) kwargs
index(*args,**kwargs)#index(1,5,z=1,r=3,t=4)
#index(*(1,5),**{1,5,z=1,r=3,t=4})
wrapper(1,5,z=1,r=3,t=4)
2、练习把参数包给函数10遍
步骤一:准备好一个函数,该函数体代码需要一个参数
def func():
print(x)
def func():
print(x)
def func():
print(x)
def func():
print(x)
def func():
print(x)
def func():
print(x)
def func():
print(x)
def func():
print(x)
def func():
print(x)
def fun():
print(x)
def func():
print(x)
步骤二:包值
x = 2
def func():
print(x)
x = 2
def func():
print(x)
x = 2
def func():
print(x)
x = 2
def func():
print(x)
x = 2
def func():
print(x)
x = 2
def func():
print(x)
x=2
def func():
print(x)
x = 2
def func():
print(x)
x=2
def func():
print(x)
x=2
def fun():
print(x)
x=2
def func():
print(x)
步骤三:闭到outter函数内
def outter():
x = 2
def func():
print(x)
def outter():
x=2
def func():
print(x)
def outter():
x=2
def func():
print(x)
def outter():
x=2
def func():
print(x)
def outter():
x=2
def func():
print(x)
def outter():
x=2
def func():
print(x)
def outter():
x=2
def func():
print(x)
def outter():
x=2
def func():
print(x)
def outter():
x=2
def func():
print(x)
def outter():
x=2
def func():
print(x)
步骤四:基于return将内部func返回到全局
def outter():
x = 2
def func():
print(x)
return func # 一定不要加括号
f = outter()
def outter():
x=2
def func():
print(x)
return func
f=outter()
def outter():
x=2
def func():
print(x)
return func
f=outter()
def outter():
x=2
def func():
print(x)
return func
f=outter()
def outter():
x=2
def func():
print(x)
return func
f=outter()
def outter():
x=2
def func():
print(x)
return func
f=outter()
def outter():
x=2
def func():
print(x)
return func
f = outter()
def outter():
x=2
def func():
print(x)
return func
f = outter()
def outter():
x=2
def func():
print(x)
return func
f=outter()
def outter():
x=2
def func():
print(x)
return func
f=outter()
0步骤五:最终格式,把outter写活
def outter(x):
# x = 2 # 注释掉
def func():
print(x)
return func
f1 = outer(1) # 把x=1包给函数func
f2 = outer(2) # 把x=2包给函数func
f3 = outer(3) # 把x=3包给函数func
def outter(x):
# x=2
def func():
print(x)
return func
f1 = outter(1)
f2 = outter(2)
f3 = outter(3)
def outter(x):
# x=2
def func():
print(x)
return func
f1 = outter(1)
f2=outter(2)
f3=outter(3)
def outter(x):
#x=2
def func():
print(x)
return func
f1 = outter(1)
f2 = outter(2)
f3 = outter(3)
def outter(x):
#x=2
def func():
print(x)
return func
f1 = outter(1)
f2 = outter(2)
f3=outter(3)
def outter(x):
# x=2
def func():
print(x)
return func
f1=outter(1)
f2=outter(2)
f3=outter(3)
def outter(x):
# x=2
def func():
print(x)
return func
f1 = outter(1)
f2 = outter(2)
f3 = outter(3)
def outter(x):
# x=2
def func():
print(x)
return func
f1 = outter(1)
f2 = outter(2)
f3 = outter(3)
def outter(x):
# x=2
def func():
print(x)
return func
f1 = outter(1)
f2 = outter(2)
f3 = outter(3)
def outter(x):
#x=2
def func():
print(x)
return func
f1 = outter(1)
f2 = outter(2)
f3 = outter(3)
def outter(x):
# x=2
def func():
print(x)
return func
f1 = outter(1)
f2 = outter(2)
f3 = outter(3)
二:作业要求:下述所有代码画出名称空间嵌套图以及分析代码执行流程
1、以定义阶段为准,先画出名称空间的嵌套关系图
2、然后找到调用函数的位置,写出函数调用时代码的执行过程,涉及到名字的查找时,参照1中画好
的嵌套图,标明查找顺序,一层一层直到找到位置
=题目一=
def func():
func()
名称空间的嵌套关系:
=题目二=
def func():
print(x)
x = 111
func()
x=111在全局空间
func()在局部空间
print(x)在局部空间,局部空间找不到,在全局空间找到的,
# =题目三=
x = 1
def func():
print(x)
def foo():
x = 222
func()
foo()
x=1在全局空间
print(x)在局部空间没找到变量,在全局空间找到。
foo()函数内调用func函数,所以打印1
# =题目四=
def f1():
def f2():
f2()
f1()
# =题目五=
x = 111
def func():
print(x)
x = 222
func()
func函数报错,x变量在打印之后
注释x=222就不会报错了,然后再全局变量中找到x=111
# =题目六=
x = 111
def foo():
print(x, )
def bar():
print(x)
foo()
bar()
foo函数局部内没有变量,从全局变量中找到x=111
bar函数局部内没有变量,从全局变量中找到x=1111
# =题目七=
x = 1
def func2():
func1()
x = 2
def func1():
print(x)
x = 3
func2()
func2函数调用func1函数
func1函数局部空间内没有变量,向外找,找到x=3,所以打印3.注意:惯性思维向上找是错误的
# =题目八=
1、如下全局变量记录了当前登录用户,编写登录功能,一旦用户登录成功,则将全局变量赋值为当前登录的用户名
login_user = None
2、针对之前编写的查询余额的功能,添加额外的逻辑:如果用户没有登录,则先执行登录功能
def login():
if user == 'liu' and pwd == '123':
# 若用户登录成功,给user_info赋值,证明用户已登录
global user_info # 函数内部可以修改外部的不可变类型的变量
user_info = user
print('login success!')
else:
print('login error!')
login()
def check_money(user):
if user_info:
db_path = 'a.txt'
with open(db_path, 'rt', encoding='utf-8') as f:
# 1)将db_path文件数据一行一行读取出来,将所有用户添加到列表
for lines in f:
# 2)判断如果user 在 在当前这行数据中,证明找到该用用户
if user in lines:
user, pwd, money = lines.strip().split('😂
print(money)
else:
print('用户没登录,请先登录!')
login()
check_money('liu')