python :装饰器
2021/9/5 1:06:06
本文主要是介绍python :装饰器,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
定义一个类:Person类, name , age
person = Person("", 19)
print(person) => “My name is xxx, age is xx”
bool(person) => 返回False
len(person) => 1024
person2 = Person("", 20)
person == person2 #如果两个年龄相等返回True, 不相等返回False
person < person2 #如果person年龄> person2的年龄,返回False, 否则 返回True
person + person2: 返回: person的名字和person2的名字
class person(): def __init__(self,name,age): self.name = name self.age = age def print(self): print(f'my name is {person.name}, age is {person.age}') def __bool__(self): return False def __len__(self): return 1024 def __eq__(self, other): if self.age == other.age: return True else: return False def __gt__(self, other): if self.age > other.age: return False else: return True def __add__(self, other): return self.age + other.age zhangsan = person('zhangsan', 20) lisi = person('lisi', 22) print(zhangsan) bool(zhangsan) len(zhangsan) print(zhangsan == lisi) print(zhangsan < lisi) print(zhangsan + lisi)
定义一个装饰器:计算函数执行时间: 使用模块time
time.sleep(2)
#计算函数运行时间;
import time
print(time.time())
time.sleep(2)
print(time.time())
import time a = time.time() def fun_a(aaa): def fun2(): print('I',end='\t') aaa() print('YOU') return fun2 @fun_a def fun(): print('LOVE',end='\t') fun() time.sleep(5.20) b = time.time() print(f"{b - a:4.3}")
这篇关于python :装饰器的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-09-18初探Python股票自动化交易:入门指南
- 2024-09-18Python量化入门:轻松掌握量化分析基础与实战
- 2024-09-18Python量化交易:入门指南与实践
- 2024-09-18Python量化交易:入门指南与实战技巧
- 2024-09-14Python人工智能项目实战:从零开始的实践指南
- 2024-09-14探索Python人工智能资料:初学者的指南
- 2024-09-14Python人工智能资料:初学者的全面指南
- 2024-09-13Matplotlib入门:轻松绘制Python数据可视化图表
- 2024-09-13Python人工智能:初学者的入门指南
- 2024-09-13Python人工智能:轻松入门与实践