Python 装饰器实战——@property、@staticmethod、自定义装饰器 装饰器是 Python 的核心特性之一。这篇讲内置装饰器的用法和自定义装饰器的实现。一、propertyclassStudent:def__init__(self,name,score):self.namename self._scorescorepropertydefscore(self):returnself._scorescore.setterdefscore(self,value):ifvalue0orvalue100:raiseValueError(成绩必须在0-100之间)self._scorevalue二、staticmethodclassUtils:staticmethoddefis_valid_email(email):returninemailand.inemail三、自定义装饰器deflog_time(func):defwrapper(*args,**kwargs):importtime starttime.time()resultfunc(*args,**kwargs)print(f{func.__name__}耗时{time.time()-start:.3f}秒)returnresultreturnwrapperlog_timedefslow_function():importtime time.sleep(1)return完成 觉得有用的话点赞 关注【张老师技术栈】吧