469,626 Members | 871 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,626 developers. It's quick & easy.

__dict__ strangeness

Hi,

can someone please tell me that this is correct and why:
class C(object): .... pass
.... c = C()
c.a = 1
c.__dict__ {'a': 1} c.__dict__ = {}
c.a Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: 'C' object has no attribute 'a'
class D(object): .... __dict__ = {}
.... d = D()
d.a = 1
d.__dict__ {} d.__dict__ = {}
d.a

1

Thanks,
Georg
Mar 18 '06 #1
7 1382
Georg Brandl on comp.lang.python said:
d.__dict__

{}


Which Python version, on which system? I can see the properly inserted
attribute in __dict__, both with old style and new style classes.
--
Alan Franzoni <al***************@gmail.com>
-
Togli .xyz dalla mia email per contattarmi.
Rremove .xyz from my address in order to contact me.
-
GPG Key Fingerprint:
5C77 9DC3 BD5B 3A28 E7BC 921A 0255 42AA FE06 8F3E
Mar 18 '06 #2
Alan Franzoni wrote:
Georg Brandl on comp.lang.python said:
> d.__dict__

{}


Which Python version, on which system? I can see the properly inserted
attribute in __dict__, both with old style and new style classes.


It's 2.4.2, on Linux. The 2.5 SVN trunk has the same symptom.

Georg
Mar 18 '06 #3
Georg Brandl <g.*************@gmx.net> wrote:
can someone please tell me that this is correct and why:


IMHO, it is not correct: it is a Python bug (and it would be nice to fix
it in 2.5).
class C(object): ... pass
... c = C()
c.a = 1
c.__dict__ {'a': 1} c.__dict__ = {}
c.a Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: 'C' object has no attribute 'a'


So far so good, I think we agree;-).
class D(object): ... __dict__ = {}
... d = D()
d.a = 1
d.__dict__ {} d.__dict__ = {}
d.a

1


Yep, that's the bug, fully reproducible in 2.3 and 2.4. FWIW, mucking
around with gc.getreferrers (with a more uniquely identifiable value for
d.a;-) shows a dictionary "somewhere" with keys 'a' and '__dict__'...
Alex
Mar 18 '06 #4
[moving to python-dev]

Alex Martelli wrote:
Georg Brandl <g.*************@gmx.net> wrote:
can someone please tell me that this is correct and why:


IMHO, it is not correct: it is a Python bug (and it would be nice to fix
it in 2.5).


Fine. Credits go to Michal Kwiatkowski for discovering that in bug #1448042
which I closed out of ignorance ;)
>>> class C(object):

... pass
...
>>> c = C()
>>> c.a = 1
>>> c.__dict__

{'a': 1}
>>> c.__dict__ = {}
>>> c.a

Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: 'C' object has no attribute 'a'


So far so good, I think we agree;-).

Yes.
>>> class D(object):

... __dict__ = {}
...
>>> d = D()
>>> d.a = 1
>>> d.__dict__

{}
>>> d.__dict__ = {}
>>> d.a

1


Yep, that's the bug, fully reproducible in 2.3 and 2.4. FWIW, mucking
around with gc.getreferrers (with a more uniquely identifiable value for
d.a;-) shows a dictionary "somewhere" with keys 'a' and '__dict__'...


Georg
Mar 18 '06 #5
Georg Brandl <g.*************@gmx.net> wrote:
[moving to python-dev]

Alex Martelli wrote:
Georg Brandl <g.*************@gmx.net> wrote:
can someone please tell me that this is correct and why:


IMHO, it is not correct: it is a Python bug (and it would be nice to fix
it in 2.5).


Fine. Credits go to Michal Kwiatkowski for discovering that in bug #1448042
which I closed out of ignorance ;)


Yep, I remember Michal posting about the bug here, and I suggested that
he open it as an actual bug on SF;-)... [[I'm supposed to be finishing
up the 2nd edition of the Nutshell, so I can't really do much on Python
myself, these days...]]
Alex
Mar 18 '06 #6

Georg Brandl wrote:
Hi,

can someone please tell me that this is correct and why:
class C(object): ... pass
... c = C()
c.a = 1
c.__dict__ {'a': 1} c.__dict__ = {}
c.a Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: 'C' object has no attribute 'a'
class D(object): ... __dict__ = {}
... d = D()
d.a = 1
d.__dict__ {} d.__dict__ = {}
d.a 1

Thanks,
Georg


Here is another example that might help:
class E(object): .... __dict__ = {'a': 1}
.... e = E()
e.__dict__ {'a': 1} E.__dict__ <dictproxy object at 0x00A81930> E.__dict__['a'] Traceback (most recent call last):
File "<stdin>", line 1, in ?
KeyError: 'a' E.__dict__['__dict__']

{'a': 1}

Ziga

Mar 18 '06 #7
Georg Brandl on comp.lang.python said:
It's 2.4.2, on Linux. The 2.5 SVN trunk has the same symptom.


Yes, I confirm this. I think I had done something strange on my system
(probably misspelled something).

--
Alan Franzoni <al***************@gmail.com>
-
Togli .xyz dalla mia email per contattarmi.
Rremove .xyz from my address in order to contact me.
-
GPG Key Fingerprint:
5C77 9DC3 BD5B 3A28 E7BC 921A 0255 42AA FE06 8F3E
Mar 20 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Ed Young | last post: by
1 post views Thread by anabell | last post: by
1 post views Thread by Derek Fountain | last post: by
5 posts views Thread by Jean Brouwers | last post: by
8 posts views Thread by Steven Bethard | last post: by
2 posts views Thread by Robert M. Gary | last post: by
8 posts views Thread by Steven D'Aprano | last post: by
12 posts views Thread by Ivan Voras | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.