Python之函数对象、函数嵌套、名称空间与作用域、闭包函数、装饰器

时间:2023-03-09 03:00:13
Python之函数对象、函数嵌套、名称空间与作用域、闭包函数、装饰器

目录


一、函数对象

1.函数是第一类对象

#第一类对象的特性:
# 可以被引用
# 可以当做参数传递
# 返回值是函数
# 可以当做容器类型的元素
# def func():
# print('from func')
#可以被引用
# f=func()
#可以被当做参数
# def bar(x):
# print(x)#func内存地址
# x()
# bar(func) #当做返回值
# def bar(x):#x=func
# return x #return func
#
# res=bar(func)#res=func
# print(res)
# res()

2.取代多分支if

用多分支if时

 def auth():
print('登陆。。。') def register():
print('注册。。。') def search():
print('查看。。。') def interactive():
while True:
print('''
1.登陆
2.注册
3.查看''')
choice=input('>>>').strip()
if choice=='':
auth()
elif choice=='':
register()
elif choice=='':
search()
else:
print('输入非法') l=[auth,register,search,interactive,]
l[3]()

取代多分支if时

 def auth():
print('登陆。。。') def register():
print('注册。。。') def search():
print('查看。。。') def transfer():
print('转账。。。') def interactive():
while True:
print('''
1.登陆
2.注册
3.查看
4.转账
''')
choice=input('>>>').strip()
dic={
'':auth,
'':register,
'':search,
'':transfer,
}
if choice in dic:
dic[choice]()
else:
print('输入非法') interactive()

二、函数的嵌套

1.函数的嵌套调用

def max(x,y):
return x if x > y else y def max4(a,b,c,d):
res1=max(a,b)
res2=max(res1,c)
res3=max(res2,d)
return res3
print(max4(1,2,3,4))

2.函数的嵌套定义

def func1():
print(1)
def func2():
print(2)
def func3():
print(3)
func3()
func2()
func1()

    函数嵌套调用的应用:

db_path='db.txt'
def uname():
while True:
uname = input('username>>>').strip()
if not uname.isalnum():continue
with open(r'%s'%db_path,'r',encoding='utf-8')as f:
for line in f:
if line.startswith(uname):
print('该用户已注册,请重新输入')
break
else:
return uname def upwd():
while True:
upwd1=input('password>>>').strip()
upwd2=input('confirm password>>>').strip()
if upwd1==upwd2:
return upwd1
else:
print('输入不一致,请重新输入') def balance():
balance=input('请输入充值金额>>>').strip()
if balance.isdigit():
return balance def db_handle(*args):
info=':'.join(args)
with open(r'%s'%db_path,'a',encoding='utf-8') as f:
f.write('%s\n' %(info)) def interactive():
username=uname()
password=upwd()
user_balance=balance()
return username,password,user_balance username,password,user_balance=interactive()
print(username)
print(password)
print(user_balance) db_handle(username,password,user_balance)

三、名称空间与作用域

名称空间

1.什么是名称空间,名称空间的分类及其各自特点

'''
名称空间是用于存放名字与值绑定关系的地方(内存地址)。
名称空间可分为三类:
1.内置名称空间(python解释器自带名字)
具有全局性、解释器启动生效,关闭则失效
2.全局名称空间(顶头写的,没有任何缩进、文件级别的,名称在文件执行时就生效,文件结束或名称引用计数为0的时候就失效)
不是内置的,也不是在函数内的
3.局部名称空间
函数的参数以及函数内的名字都存放在局部名称空间,在函数调用时临时生效
'''

2.名称空间的加载顺序

#1、python解释器先启动,因而首先加载的是:内置名称空间
#2、执行test.py文件,然后以文件为基础,加载全局名称空间
#3、在执行文件的过程中如果调用函数,则临时产生局部名称空间

3.名字查找的顺序

'''
查找名字(以局部为开始):
局部》全局》内置
'''
x=2
def f1():
x=3
def f2():
# x=4
def f3():
print(x)
f3()
# x=5
f2()
f1()#打印x=4

作用域

'''
作用域分为两类:
1.全局作用域:
包含:内置、全局
特点:在任何位置都能访问到、伴随程序整个生存周期
2.局部范围
包含:局部
特点:只能在函数内使用、调用函数时生效,结束调用就失效
'''

四、闭包函数

'''
闭包函数就是定义在函数内的函数,并且该函数包含对外部函数作用域中名字的引用。
闭包函数打破了原本函数嵌套的层级限制,能在外面或其它函数里用。闭包函数的作用域关系是在函数定义阶段就被规定死了,与其调用位置无关。接下来我们就来试一下:
'''
def outter():
x=3
def inner():
print('from inner',x)
return inner
x=4
f=outter()
f()#返回from inner 3

五、装饰器

为什么要用装饰器?

开放封闭原则:
对修改源代码封闭,对扩展功能开放

什么是装饰器?

装饰器就是在不修改源代码与调用方式的前提下,为被装饰对象增加新功能。
装饰器与被装饰对象均可以是任意调用的对象。

装饰器的使用:

 import time

 def timmer(func):#专门用来接收最原始的被装饰函数
#func=index
def inner(*args,**kwargs):
start_time=time.time()
res=func(*args,**kwargs)#调用最原始的被装饰函数
stop_time=time.time()
print('run time is %s'%(stop_time-start_time))
return res
return inner @timmer#index=timmer(index)将被装饰函数当作参数传给timmer
def index():
time.sleep(2)
print('welcome to index page')
return 789 @timmer#home=timmer(home)将被装饰函数当作参数传给timmer
def home(name):
time.sleep(4)
print('welcome <%s> to home page'%name)
return name
@timmer#about=timmer(about)将被装饰函数当作参数传给timmer
def about():
time.sleep(5)
print('this is a detail page')
return 456 index()
home(input('username>>>').strip())
about()

无参

 import time
userinfo={
'username':None,
}
file_path=r'db.txt'
def timmer(func):
def inner(*args,**kwargs):
start_time=time.time()
res=func(*args,**kwargs)
stop_time=time.time()
print('run time is %s'%(stop_time-start_time))
return res
return inner def wauth(engine):
def auth(func):
def inner(*args,**kwargs):
if engine=='file':
if userinfo['username']:
print('用户已登陆')
res = func(*args, **kwargs)
return res
while True:
uname=input('username>>>').strip()
pwd=input('password>>>').strip()
with open(file_path,'r',encoding='utf-8') as f:
for line in f:
user=line.strip('\n').split(',')
if uname==user[0] and pwd==user[1]:
print('登陆成功')
userinfo['username']=uname
res=func(*args,**kwargs)
return res,uname
else:
print('用户名或密码错误')
elif engine=='mysql':
print('基于MySQL认证')
elif engine=='ldap':
print('基于LDAP认证')
else:
print('认证失败')
return inner
return auth @timmer
@wauth('file')
def index():
time.sleep(3)
print('welcome to index page') @timmer
@wauth('file')
def home():
time.sleep(1)
print('welcome to home page') index()
home()

有参