Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   __dict__ strangeness (http://www.velocityreviews.com/forums/t355586-__dict__-strangeness.html)

Georg Brandl 03-18-2006 03:04 PM

__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

Alan Franzoni 03-18-2006 05:05 PM

Re: __dict__ strangeness
 
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 <alan.franzoni.xyz@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

Georg Brandl 03-18-2006 05:27 PM

Re: __dict__ strangeness
 
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

Alex Martelli 03-18-2006 05:39 PM

Re: __dict__ strangeness
 
Georg Brandl <g.brandl-nospam@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

Georg Brandl 03-18-2006 05:56 PM

Re: __dict__ strangeness
 
[moving to python-dev]

Alex Martelli wrote:
> Georg Brandl <g.brandl-nospam@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

Alex Martelli 03-18-2006 07:04 PM

Re: __dict__ strangeness
 
Georg Brandl <g.brandl-nospam@gmx.net> wrote:

> [moving to python-dev]
>
> Alex Martelli wrote:
> > Georg Brandl <g.brandl-nospam@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

Ziga Seilnacht 03-18-2006 10:25 PM

Re: __dict__ strangeness
 

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


Alan Franzoni 03-20-2006 04:57 PM

Re: __dict__ strangeness
 
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 <alan.franzoni.xyz@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


All times are GMT. The time now is 04:30 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.