-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecorator_study.py
65 lines (49 loc) · 1.71 KB
/
decorator_study.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
修饰:
@property
@staticmethod
@classmethod
"""
class Phone:
# 属性默认为类属性(即可以给直接被类本身调用)
num = 1
def __init__(self):
self._cost_price = 1999
"""
使用 @property 属性,修饰方法,可以像属性一样访问
如果使用 property 进行修饰后,又在调用的时候,方法后面添加了(),
那么就会显示错误信息:TypeError: 'int' object is not callable,
也就是说添加 @property 后,这个方法就变成了一个属性,
如果后面加入了(),那么就是当作函数来调用,而它却不是 callable(可调用)的。
作用:某个属性是通过其它属性计算得来的,
调用 myPhone.sellingPrice 比 myPhone.sellingPrice() 更自然,看起来是成员属性,
语义上更好理解
"""
@property
def sellingPrice(self):
return self._cost_price * 1.9
def getBrand(self):
return "XiaoMi"
"""
类静态方法
【易错点】类静态方法不能有 self 参数
staticmethod 第一个参数不需要传入 cls 或 self,故 staticmethod 中是无法访问类和对象的数据的。
"""
@staticmethod
def getCEO():
return "LeiJun"
"""
classmethod 的第一个参数为类本身(cls),正如实例方法的第一个参数为对象本身(self);
特点:可以访问类属性和类方法,staticmethod 就不行
"""
@classmethod
def getNum(cls):
return cls.num
def test_0():
myPhone = Phone()
print("price:", myPhone.sellingPrice)
print("CEO:", Phone.getCEO())
if __name__ == '__main__':
test_0()