Python函数(十)-装饰器(三)
如果多个函数想通过一个装饰器来实现不同的功能的话,可以给装饰器传入参数,让装饰器里的函数对参数进行判断,来实现不同的功能
# -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" def deco(info_type): def outer(func): #这在传入参数名 def inner(*args,**kwargs): if info_type == 'name': #对装饰器的参数进行判断 func(*args, **kwargs) name_info = input("your name:") print("your name is %s"%name_info) if info_type == 'age': func(*args, **kwargs) age_info = input("your age:") print("your age is %s"%age_info) return inner return outer @deco(info_type='name') #给装饰器传参数 def name(): print("input your name") @deco(info_type='age') def age(): print("input your age") if __name__ == '__main__': name() age()
运行结果