__dict__ strangeness

Discussion in 'Python' started by Georg Brandl, Mar 18, 2006.

  1. Georg Brandl

    Georg Brandl Guest

    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
    Georg Brandl, Mar 18, 2006
    #1
    1. Advertising

  2. 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 <>
    -
    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
    Alan Franzoni, Mar 18, 2006
    #2
    1. Advertising

  3. Georg Brandl

    Georg Brandl Guest

    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
    Georg Brandl, Mar 18, 2006
    #3
  4. Georg Brandl <> 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
    Alex Martelli, Mar 18, 2006
    #4
  5. Georg Brandl

    Georg Brandl Guest

    [moving to python-dev]

    Alex Martelli wrote:
    > Georg Brandl <> 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
    Georg Brandl, Mar 18, 2006
    #5
  6. Georg Brandl <> wrote:

    > [moving to python-dev]
    >
    > Alex Martelli wrote:
    > > Georg Brandl <> 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
    Alex Martelli, Mar 18, 2006
    #6
  7. 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
    Ziga Seilnacht, Mar 18, 2006
    #7
  8. 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 <>
    -
    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
    Alan Franzoni, Mar 20, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ed Young
    Replies:
    4
    Views:
    327
    Ed Young
    Aug 10, 2003
  2. Replies:
    1
    Views:
    335
    Alex Martelli
    Nov 6, 2003
  3. Derek Fountain

    When is a __dict__ not a __dict__?

    Derek Fountain, Apr 21, 2004, in forum: Python
    Replies:
    1
    Views:
    332
    John Roth
    Apr 21, 2004
  4. Jean Brouwers

    __slots__ vs __dict__

    Jean Brouwers, May 12, 2004, in forum: Python
    Replies:
    5
    Views:
    513
    Marcus von Appen
    May 13, 2004
  5. Hornberger, Chris

    RE: __slots__ vs __dict__

    Hornberger, Chris, May 12, 2004, in forum: Python
    Replies:
    1
    Views:
    424
    Jean Brouwers
    May 12, 2004
Loading...

Share This Page