|
发表于 2022-9-4 16:22:23
|
显示全部楼层
- class C:
- def __init__(self,x):
- self.x=x
-
- c=C(250)
- c.x
- 250
- c.__dict__
- {'x': 250}
- c.y=520
- c.__dict__
- {'x': 250, 'y': 520}
- c.__dict__['z']=666
- c.__dict__
- {'x': 250, 'y': 520, 'z': 666}
- c.z
- 666
- class C:
- __slots__=["x","y"]
- def __init__(self,x):
- self.x=x
-
- c=C(250)
- c.x
- 250
- c.y=520
- c.y
- 520
- c.z=666
- Traceback (most recent call last):
- File "<pyshell#287>", line 1, in <module>
- c.z=666
- AttributeError: 'C' object has no attribute 'z'
- class D:
- __slots__=["x","y"]
- def __init__(self,x,y,z):
- self.x=x
- self.y=y
- self.z=z
-
- d=D(3,4,5)
- Traceback (most recent call last):
- File "<pyshell#295>", line 1, in <module>
- d=D(3,4,5)
- File "<pyshell#294>", line 6, in __init__
- self.z=z
- AttributeError: 'D' object has no attribute 'z'
- class E(C):
- pass
- e=E(250)
- e.x
- 250
- e.y=520
- e.y
- 520
- e.z=666
- e.__slots__
- ['x', 'y']
- e.__dict__
- {'z': 666}
复制代码 |
|