博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python(^^^^^小技巧^^^^^——不定期更新)
阅读量:7122 次
发布时间:2019-06-28

本文共 4100 字,大约阅读时间需要 13 分钟。

偶然想到的小技巧

'''交互中对传入函数的参数的数目进行检测'''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:\py\empty\c.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)       #光标初始化
列表解析是能够套多层for循环的
def init(func):                   #send必须传值到yield,所以需要先将生成器暂停到一个yield,相当于每次send之前先要初始化一次。如果send的时候不是yield暂停,会报错    def wrapper(*args,**kwargs):        g=func(*args,**kwargs)        next(g)        return g    return wrapper@initdef 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))
yield生成器

# sleep(secs)    #------------------time.sleep()  I/O阻塞,(类似于input)不占cpu# 线程推迟指定的时间运行,单位为秒。
sleep(secs)

 

转载于:https://www.cnblogs.com/zihe/p/7206227.html

你可能感兴趣的文章
python装饰器
查看>>
bzoj 3196: Tyvj 1730 二逼平衡树
查看>>
Linux学习第三篇之Linux常用命令——命令格式与目录处理命令ls
查看>>
Unable to execute dex: method ID not in [0, 0xffff]: 65536
查看>>
【人生】不管你挣多少, 钱永远是问题
查看>>
DG备库无法接受主库归档日志之密码文件
查看>>
[bzoj 2555]Substring
查看>>
tab标签
查看>>
ecshop新增银联企业网银支付方式
查看>>
Angular5学习笔记 - 配置NG-ZORRO(八)
查看>>
使用Netty实现HTTP服务器
查看>>
JAVA开发工具eclipse中@author怎么改
查看>>
存储引擎与锁
查看>>
sqlog连接虚拟机mysql服务
查看>>
出错,网页显示不出内容
查看>>
Spring中的后置处理器BeanPostProcessor讲解
查看>>
《FPGA全程进阶---实战演练》第十四章 蜂鸣器操作
查看>>
浅析firmware完整生存和使用流程 【转】
查看>>
《30天自制操作系统》笔记(01)——hello bitzhuwei’s OS!【转】
查看>>
MMU介绍【转】
查看>>