-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython-buildin
More file actions
84 lines (67 loc) · 4.54 KB
/
python-buildin
File metadata and controls
84 lines (67 loc) · 4.54 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
property() 添加钩子,向后兼容 (对外发布的客户端代码,版本兼容是个要考虑的问题,服务端改变,不需要客户端做修改)
Python式的解决方式是使用property。这里是我们已经实现了的一个版本:
class Celsius:
def __init__(self, temperature = 0):
self.temperature = temperature
def to_fahrenheit(self):
return (self.temperature * 1.8) + 32
def get_temperature(self):
print("Getting value")
return self._temperature
def set_temperature(self, value):
if value < -273:
raise ValueError("Temperature below -273 is not possible")
print("Setting value")
self._temperature = value
temperature = property(get_temperature,set_temperature)
我们在get_temperature()和set_temperature()的内部增加了一个print()函数,用来清楚地观察它们是否正在执行。代码的最后一行,
创建了一个property对象temperature。简单地说,property将一些代码(get_temperature和set_temperature)附加到成员属性(temperature)的访问入口。
任何获取temperature值的代码都会自动调用get_temperature(),而不是去字典表(__dict__)中进行查找。
同样的,任何赋给temperature值的代码也会自动调用set_temperature()。
这是Python中一个很酷的功能。我们实际演示一下。
>>> c = Celsius()
Setting value
>>> c.temperature
Getting value
0
>>> c.temperature = 37
Setting value
>>> c.to_fahrenheit()
Getting value
98.60000000000001
我们可以看到,通过使用property,我们在不需要客户代码做任何修改的情况下,修改了我们的类,并实现了值约束。
因此我们的实现是向后兼容的,这样的结果,大家都很高兴。
最后需要注意的是,实际温度值存储在私有变量_temperature中。属性temperature是一个property对象,是用来为这个私有变量提供接口的。
------------------------------------------------------------------------------------------------------------------------
举个例子如下,解释器可以这么运作。首先在内存中地址为XXXX的地方存了一个变量1,然后运行到a=1时,发现整数1要赋值给变量a,
所以解释器就知道了啊哈变量a是整型,然后a其实保存的是地址XXXX。
然后运行到a=0.1,解释执行器在内存中地址为YYYY的地方存了0.1,然后知道了啊哈a是浮点类型,然后a保存的地址变为了YYYY。
a=1
a=0.1
既然解释器每次在保存变量值(指向地址)时,都要识别值得类型并关联到变量,所以效率肯定会低一点吧。
(如果解释执行器是这么运作的话,Python可是有不止一种解释执行器,所以也得看开发解释执行器的人的设计啦)。
四、id() is() 与 ==
id()函数非常好理解,就是求变量地址,例子如下,注意同样是值1,可能对应的地址是不同的,因为在内存为1分配地址时分配了不同地址。
a=1
print(id(a))#输出3543160
了解了id(),那么is()和==的区别就好理解了。==就是看值相等不,相等就返回True。而is()就是看id相等不,相等就返回True。
举个例子:
a=1
b=1.0
a==b#True,值就是相等哦
a is b#False,地址肯定不一样
c=1
a is c#True,这个应该看解释器了,如果解释器为常量1分配一样的地址就相等了,也有可能解释器特殊喜欢分配不同地址。
id(object)
Return the “identity” of an object. This is an integer which is guaranteed to be unique and constant for this object during its lifetime.
Two objects with non-overlapping lifetimes may have the same id() value.
CPython implementation detail: This is the address of the object in memory.
All of Python’s immutable built-in objects are hashable; mutable containers (such as lists or dictionaries) are not.
Objects which are instances of user-defined classes are hashable by default. They all compare unequal (except with themselves),
and their hash value is derived from their id().
Set Types — set, frozenset
集合类型 —— 可变集合,不可变集合
A set object is an unordered collection of distinct hashable objects. Common uses include membership testing,
removing duplicates from a sequence, and computing mathematical operations such as intersection, union, difference,
and symmetric difference. (For other containers see the built-in dict, list, and tuple classes, and the collections module.)
----------------------------------------------------------------------------------------------------------------------------------