偶然想到的小技巧
''' 交互中对传入函数的参数的数目进行检测 ''' def func(a,b,c): print(a,b,c) s=input(">>>>:") s=s.split() t=tuple(s) try: #Python本身会检测输入函数的参数数目是否对应, func(*t) #*[元组]的方式传参数 #如果不对应,会报出TypeError, except TypeError: # 所以可以捕获typeerror来方便的进行参数数目的检测 print("参数错误") #**kwargs 的方式传参也可以
#===================================================================> # def func(x,y,*args): #args=(3,4,5,6) # print(x,y) # print(args) # # func(1,2,*(3,4,5,6)) #foo(1,2,3,4,5,6) #这样传参和解开元组后传参效果是一样的 #=====================================================================================> # def func(x,y,**kwargs): #x=1,y=2,**kwargs=**{'a':1,'b':3,'z':3} # print(x,y) # print(kwargs) # # func(1,y=2,**{'a':1,'b':3,'z':3}) #func(1,y=2,z=3,b=3,a=1) #这样传参就和解开字典后传参,效果一样 #不加*、**,元组、字典就没有响应的功能,只能传给一个形参
#函数是第一类对象: 指的是函数可以被当做数据传递 # def func(): # print('from func') #可被引用 # f=func #可以当做函数的参数 # def func(): # print('from func') # def foo(x): # print(x) # x() # foo(func) #可以当做函数的返回值 # def foo(): # print('from foo') # def bar(): # return foo # f=bar() # print(f) # print(foo) # f() # x=0 # def f1(): # x=1 # def f2(): # # x=2 # print(x) # return f2 # f=f1() # print(f) # f() #可以当做容器类型的元素 # def select(): # print('select function') # # func_dic={ # 'select':select, # } # # print(func_dic['select']) # func_dic['select']() # # # def select(): # print('select func') # # def delete(): # print('delete func') # # def change(): # print('change func') # # def add(): # print('add func') # # # while 1: # cmd=input('>>: ').strip() # if not cmd:continue # if cmd == 'select': # select() # elif cmd == 'delete': # delete() # elif cmd == 'change': # change() # elif cmd == 'add': # add() # else: # print('无效的命令') def select(cmd_l): filename=cmd_l[-1] pattern=cmd_l[1] with open(filename,'r',encoding='utf-8') as f: for line in f: if pattern in line: print(line) def delete(): print('delete func') def change(): print('change func') def add(): print('add func') def check(): print('check func') func_dic={ 'select':select, 'delete':delete, 'change':change, 'add':add, 'check':check, } while 1: inp=input('>>: ').strip() if not inp:continue #如果输入为空则继续 输入自带bool值,如果为空,bool值为FALSE,不空则为True。 cmd_l=inp.split() # print(cmd_l) cmd=cmd_l[0] if cmd in func_dic: func_dic[cmd](cmd_l) else: print('无效的命令')
#有参装饰器,在一般装饰器之外添加针对装饰函数的参数,增加了新参数,所以再加包一层 def deco(auth_type='file'): def auth(func): def wrapper(*args,**kwargs): if auth_type == 'file': print('文件的认证方式') elif auth_type == 'ldap': print('ldap认证方式') elif auth_type == 'mysql': print('mysql认证方式') else: print('不知到的认证方式') return wrapper return auth @deco(auth_type='abc') #@auth #index=auth(index) def index(): print('welecome to index') @deco(auth_type='ldap') def home(name): print('welecome %s to home page' %name) index() home('egon')
#直接使用地址调用函数能够跳过装饰器
with open(r'D:pyemptyc.txt','r',encoding='utf-8') as f: print(sum([float(i.split()[1])*int(i.split()[2]) for i in f])) f.seek(0) #光标初始化 print([item for line in f for item in line.split() if item.isdigit()]) f.seek(0) #光标初始化
def init(func): #send必须传值到yield,所以需要先将生成器暂停到一个yield,相当于每次send之前先要初始化一次。如果send的时候不是yield暂停,会报错 def wrapper(*args,**kwargs): g=func(*args,**kwargs) next(g) return g return wrapper @init def eater(name): print('%s ready to eat' %name) food_list=[] while True: food=yield food_list #send传入的值给了yield,赋值给了food,但是执行结束的时候,返回值是food_list,和yield本身传入的值没有关系。 food_list.append(food) print('%s start to eat %s' %(name,food))
# sleep(secs) #------------------time.sleep() I/O阻塞,(类似于input)不占cpu # 线程推迟指定的时间运行,单位为秒。