0
>>> class Triangle(object):
... number_of_sides = 3
... def __init__(self, angle1, angle2, angle3):
... self.angle1 = angle1
... self.angle2 = angle2
... self.angle3 = angle3
... def check_angles(self):
... return True if self.angle1 + self.angle2 + self.angle3 == 180 else False
...
>>> class Equilateral(Triangle):
... angle = 60
... def __init__(self):
... self.angle1 = angle
... self.angle2 = angle
... self.angle3 = angle
...
>>>
>>> e = Equilateral()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in __init__
NameError: global name 'angle' is not defined
令人驚訝的是這段代碼給出了一個例外。爲什麼發現angle
未定義?實例方法中類成員變量可見性
問題是不是我怎樣才能訪問angle
,問題是爲什麼angle
無法訪問?
use'ClassName.class_attribute' –