>>> class Foo:
... 'it is a example'
... print 'i am here'
...
i am here
>>> Foo.__name__
'Foo'
>>> Foo().__name__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: Foo instance has no attribute '__name__'
>>> Foo.__doc__
'it is a example'
>>> Foo().__doc__
'it is a example'
>>> Foo.__dict__
{'__module__': '__main__', '__doc__': 'it is a example'}
>>> Foo().__dict__
{}
>>> Foo.__module__
'__main__'
>>> Foo().__module__
'__main__'
>>> myname=Foo()
>>> myname.__name__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: Foo instance has no attribute `__name__`
Aus welchem Grund haben Instanzen kein Attribut __name__
?
vielleicht ist es in Ordnung, dass die __name__
von instance-myname myname
ist.
Würdest du mir etwas logischer sagen, nicht die unvernünftigen Grammatikregeln?Warum haben Python-Instanzen kein Attribut __name__?
Was lässt Sie vermuten, dass ein Attribut "__name__" für Instanzen vorhanden sein soll? Die [relevante Dokumentation] (http://docs.python.org/2/reference/datamodel.html) sagt, dass es für Klassen (und andere Dinge) existiert, aber ich sehe keine Erwähnung davon für Instanzen. – Celada
Aus der [docs] (http://docs.python.org/2/library/stdtypes.html?highlight=__name__#class.__name__): '__name__' ist der Name einer" Klasse oder eines Typs ", nicht ein Instanz einer Klasse oder eines Typs. Warum sollten Instanzen '__name__'s haben? –
Meintest du 'Foo() .__ class __.__ name__'? –