解析一下装饰器

装饰器:所谓的装饰器,就是为当前函数添加修改一些功能的函数

使用方法:在要修改的函数上面加上@装饰器

python中常用的装饰器:①property ②staticmethod ③classmethod

接下来我们来看三种装饰器的作用,这里将不使用汉字进行描述,大家看输出即可。

令:这里并没有想让没接触过的人直接看懂,而是要留下一个“哦!我确实看到了这个装饰器改变了某些东西!”的印象,然后我将会从最基础的角度解析装饰器的一生。

①property例子如下:

 1 class A():
 2     name = "test"
 3     
 4     def __init__(self, name):
 5         self.name = name
 6     
 7     @property
 8     def goout(self )
 9         print(self.name)
10         
11 a = A()
12 a = A("chensang")
13 print(a.name)
14 print('==========')
15 a.goout
16 print("???,发生了什么?goout后面不用加小括号的吗?")

  

猜你喜欢

转载自www.cnblogs.com/chensang/p/9920885.html