Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > __dict__ strangeness

Reply
Thread Tools

__dict__ strangeness

 
 
Georg Brandl
Guest
Posts: n/a
 
      03-18-2006
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
 
Reply With Quote
 
 
 
 
Alan Franzoni
Guest
Posts: n/a
 
      03-18-2006
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 <(E-Mail Removed)>
-
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
 
Reply With Quote
 
 
 
 
Georg Brandl
Guest
Posts: n/a
 
      03-18-2006
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
 
Reply With Quote
 
Alex Martelli
Guest
Posts: n/a
 
      03-18-2006
Georg Brandl <(E-Mail Removed)> 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
 
Reply With Quote
 
Georg Brandl
Guest
Posts: n/a
 
      03-18-2006
[moving to python-dev]

Alex Martelli wrote:
> Georg Brandl <(E-Mail Removed)> 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
 
Reply With Quote
 
Alex Martelli
Guest
Posts: n/a
 
      03-18-2006
Georg Brandl <(E-Mail Removed)> wrote:

> [moving to python-dev]
>
> Alex Martelli wrote:
> > Georg Brandl <(E-Mail Removed)> 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
 
Reply With Quote
 
Ziga Seilnacht
Guest
Posts: n/a
 
      03-18-2006

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

 
Reply With Quote
 
Alan Franzoni
Guest
Posts: n/a
 
      03-20-2006
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 <(E-Mail Removed)>
-
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
__slots__ vs __dict__ Jean Brouwers Python 5 05-13-2004 04:20 PM
RE: __slots__ vs __dict__ Hornberger, Chris Python 1 05-12-2004 09:35 PM
When is a __dict__ not a __dict__? Derek Fountain Python 1 04-21-2004 10:31 AM
__slots__ replacing __dict__ function anabell@sh163.net Python 1 11-06-2003 09:16 AM
obj.__dict__ expected behavior or bug? Ed Young Python 4 08-10-2003 10:50 AM



Advertisments