Trouple Zugriff auf Python-Oberklassenattribute [duplizieren]
Diese Frage hat hier bereits eine Antwort:
Python-Methodenname mit doppeltem Unterstrich wird überschrieben? 2 AntwortenIch habe zwei Klassen, die lose die folgende Form annehmen:
class Foo:
def __init__(self, foo):
self.__foo = foo
class Bar(Foo):
def bar(self):
print self.__foo
Wenn ich versuche, das @ aufzurufbar
Methode auf einer Instanz vonBar
, es schlägt fehl
b = Bar('foobar')
b.bar()
Ergebnis
Traceback (most recent call last):
File "foobar.py", line 14, in <module>
b.bar()
File "foobar.py", line 10, in bar
print self.__foo
AttributeError: Bar instance has no attribute '_Bar__foo'
ein Verständnis ist, dass dieser Code auf der Basis von @ funktionieren solltzwe ander Fragen, warum nicht?