[issue7968] __dict__ Exception using class attributes

Andrew Shuiu report at bugs.python.org
Fri Feb 19 22:40:30 CET 2010


New submission from Andrew Shuiu <asuiu at bitdefender.com>:

Interpreter do not fill in __dict__ attribute of a class which has atributes. dir() shows them, but not __dict__. It works only when attributes are created dynamically at runtime, such as class.attribute = value, not in class definition.
Behaviour is the same on py2.6.4 and py3.1.

Python 3.1.1 (r311:74483, Aug 17 2009, 17:02:12) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> class MyClass:
...     myattr = 0
...
>>> inst = MyClass()
>>> print(inst.__dict__["myattr"])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'myattr'
>>>

but in this code it works fine:

class MyClass:
     myattr = 0
inst = MyClass()
inst.myattr = 1
print(inst.__dict__["myattr"])

So __dict__ do not contain the attributes of an instantiated object, as it should be, according to documentation.

----------
components: Build, Windows
messages: 99595
nosy: asuiu
severity: normal
status: open
title: __dict__ Exception using class attributes
type: behavior
versions: Python 2.6, Python 3.1

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue7968>
_______________________________________


More information about the Python-bugs-list mailing list