[python基础]关于装饰器
在面试的时候,被问到装饰器,在用的最多的时候就@classmethod ,@staticmethod,开口胡乱回答想这和C#的static public 关键字是不是一样的,等面试回来一看,哇,原来是这样,真佩服我当时厚着脸皮回答的那些问题...
OK,先来张图看看装饰器内容:
OK,我们留下一个印象,然后我们看实际的场景来操作.
我们先看一个方法:
1 2 3 4 5 6 7 8 9 | __author__ = 'bruce' def do_sth(): print 'some thing has been done' if __name__ = = '__main__' : do_sth() |
假定一个方法,我们现在提出一个需求,将每个方法追加一个写日志的功能,这样方便我们统计。
第一次更改:硬代码
__author__ = 'bruce' def do_sth(): print 'the function is begin' print 'some thing has been done' print 'the function is end' if __name__ == '__main__': do_sth()
看样子是符合需求,但是这里一个问题,现在只是针对特定的do_sth()方法了,那如果我们还有do_first_sth(),do_second_sth()......等更多的方法,难道我们每一个都是都去copy然后在去粘贴这些内容吗?
第二次更改:灵活变动
__author__ = 'bruce' def do_sth(): print 'some thing has been done' def log_sth(func): print 'the function is begin' func() print 'the function is end' if __name__ == '__main__': log_sth(do_sth)
这样,方法被当作参数一样传递给log_sth,这样我们只需要调用log_sth就可以灵活的写日志了,在逻辑上当然没有问题了,假如do_sth()被N处调用,那我们是不是必须得把do_sth()在N处都要更改过来,那假如你的这个函数是给别人使用的,你这样修改直接导致别人无法使用了。
第三次更改:内置引用
__author__ = 'bruce' def do_sth(): print 'some thing has been done' def log_sth(func): def wrapper(): print 'the function is begin' func() print 'the function is end' return wrapper if __name__ == '__main__': foo = log_sth(do_sth) foo()
这样就内置的实现了一个函数调用另一个函数,我们可以很形象的手中有两张牌然后插入另外一张牌,很像一个横切面,这个就叫叫面向切面编程(AOP),每次调用dosth时,都从上到下顺序的执行了外层函数,从语义上来看do_sth这个方法被log_sth这个词语修饰了,这种行为在python就是装饰器,python有更优雅的方法实现这个操作
__author__ = 'bruce' def log_sth(func): def wrapper(): print 'the function is begin' func() print 'the function is end' return wrapper @log_sth def do_sth(): print 'some thing has been done' if __name__ == '__main__': do_sth()
@log_sth就像是形容词一样,放置在do_sth函数的上方
有人会说,你这只是针对于没有参数的函数,那如果有参数的函数应该怎么写呢?
第四次变更:针对有参数的函数的装饰器
__author__ = 'bruce' def log_sth(func): def wrapper(*args, **kwargs): print 'the function is begin' func(*args, **kwargs) print 'the function is end' return wrapper @log_sth def do_sth(sth=None): if not sth: sth = 'there is nothing' print sth if __name__ == '__main__': do_sth()
OK,通过上面一些简单的实例,我们了解到装饰器的一些实际应用,关于更多的例子请参考如下的列表:
Python的Decorator 杂谈Python装饰器学习(九步入门)理解Python中的装饰器
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?