python3基础09(装饰器的使用)

时间:2021-10-12 20:45:29
#!/usr/bin/env python
# -*- coding:utf-8 -*- import time #方式1
class People: def __init__(self, name, age):
self.__name = name
self.__age = age def get_age1(self):
return self.__age def set_age2(self, age):
if isinstance(age, int):
self.__age = age
else:
raise ValueError def del_age3(self):
print("删除年龄数据!") # 核心在这句
age1 = property(get_age1, set_age2, del_age3, "年龄") obj = People("jack", 18)
print(obj.age1)
obj.age = 19
print("obj.age: ", obj.age)
del obj.age #方式2
def deco(f):
def wrapper():
start_time = time.time()
f()
end_time = time.time()
execution_time = (end_time - start_time)*1000
print("time is %d ms" %execution_time )
return wrapper @deco
def f():
print("hello")
time.sleep(1)
print("world") if __name__ == '__main__':
f()