定义一个函数,其中写出非智能手机的功能,为手机增加现在智能手机有的新功能
def decorator(func):
def add_function(*args,**kwargs):
func(*args,**kwargs)
print(“王者荣耀,发微信,登QQ,绝地求生”)
return add_function
@decorator
def phone():
print(“The phone can phone,send the message,play the box and go to the Internet”)
phone()
- decorator函数的参数传入的是phone函数;
- add_function函数的参数传入的是phone函数的参数;因为*args和**args都是可变参数,所以即使phone函数中无参数,也不会影响运行;
- func(*args,**args)代表phone()
- 之后的是新增加的功能,新增加的功能必须放在旧功能之后
- return 把add_function当做值返回给decorator函数。
- 然后用@符号将decorator函数注入phone中
- 执行的时候直接调用phone函数即可