Python(^^^^^小技巧^^^^^——不定期更新)
2024-06-09 21:16:37
偶然想到的小技巧
''' 交互中对传入函数的参数的数目进行检测 ''' 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 wrapperreturn 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 greturn 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))
yield生成器
# sleep(secs) #------------------time.sleep() I/O阻塞,(类似于input)不占cpu # 线程推迟指定的时间运行,单位为秒。
sleep(secs)
转载于:https://www.cnblogs.com/zihe/p/7206227.html
最新文章
- 互联网流量太贵!新零售成创业新风口
- 2.7 程序示例--多分类问题-机器学习笔记-斯坦福吴恩达教授
- python json模块
- java读取excel数据的方法是_java怎么读取excel文件里的数据
- redis的简单安装和配置文件的参数
- nodejs学习笔记-入门helloworld
- Unity手游开发与实战
- JavaScript中Array 对象相关的几个方法
- windows中ping端口:tcping命令
- .net的commandname领悟
- 有哪些免费的软件资源分享网站,软件资源网站大全导航
- 分享一个开源免费、功能强大的视频播放器库
- IE5,IE6,IE7,IE8的css兼容性列表
- 交警对开车人的“真言”
- H2O的机器学习框架的数据分析应用
- cs1.6 服务器制作,反恐精英CS1.6服务器建设简明手册
- 深度学习-吴恩达 作业 Tensorflow环境部署
- 第17课:强化学习的模型训练
- python作者的诗_《全唐诗》Python文本分析
- arduino 源码分层浅析
热门文章
- 【转】VB中NEW的用法(申请内存空间)
- rpm deb命令集合
- [原创]物探小宽线坐标快速生成(平行线坐标互算 、点到线距离计算、两线交点计算等)...
- 这套GitHub 1300星的NLP课程即将完结,视频授课,在线答疑丨课程
- 悄悄告诉你,在硅谷,有一种工作比程序员挣得多
- xxx/labelKeypoint/utils/qt.py:81: RuntimeWarning: invalid value encountered in double_scalars
- Codeforces Round #321 (Div. 2)
- 亲自动手用HTK实现YES NO孤立词识别
- 随机生成A~Z的字母CharDemo
- 甚至有些还掉到书本上