__dict__ strangeness

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Georg Brandl

    #1

    __dict__ strangeness

    Hi,

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

    Thanks,
    Georg
  • Alan Franzoni

    #2
    Re: __dict__ strangeness

    Georg Brandl on comp.lang.pytho n said:
    [color=blue][color=green][color=darkred]
    >>>> d.__dict__[/color][/color]
    > {}[/color]

    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

    Comment

    • Georg Brandl

      #3
      Re: __dict__ strangeness

      Alan Franzoni wrote:[color=blue]
      > Georg Brandl on comp.lang.pytho n said:
      >[color=green][color=darkred]
      >>>>> d.__dict__[/color]
      >> {}[/color]
      >
      > Which Python version, on which system? I can see the properly inserted
      > attribute in __dict__, both with old style and new style classes.[/color]

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

      Georg

      Comment

      • Alex Martelli

        #4
        Re: __dict__ strangeness

        Georg Brandl <g.brandl-nospam@gmx.net> wrote:
        [color=blue]
        > can someone please tell me that this is correct and why:[/color]

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

        So far so good, I think we agree;-).
        [color=blue][color=green][color=darkred]
        > >>> class D(object):[/color][/color]
        > ... __dict__ = {}
        > ...[color=green][color=darkred]
        > >>> d = D()
        > >>> d.a = 1
        > >>> d.__dict__[/color][/color]
        > {}[color=green][color=darkred]
        > >>> d.__dict__ = {}
        > >>> d.a[/color][/color]
        > 1[/color]

        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

        Comment

        • Georg Brandl

          #5
          Re: __dict__ strangeness

          [moving to python-dev]

          Alex Martelli wrote:[color=blue]
          > Georg Brandl <g.brandl-nospam@gmx.net> wrote:
          >[color=green]
          >> can someone please tell me that this is correct and why:[/color]
          >
          > IMHO, it is not correct: it is a Python bug (and it would be nice to fix
          > it in 2.5).[/color]

          Fine. Credits go to Michal Kwiatkowski for discovering that in bug #1448042
          which I closed out of ignorance ;)
          [color=blue][color=green][color=darkred]
          >> >>> class C(object):[/color]
          >> ... pass
          >> ...[color=darkred]
          >> >>> c = C()
          >> >>> c.a = 1
          >> >>> c.__dict__[/color]
          >> {'a': 1}[color=darkred]
          >> >>> c.__dict__ = {}
          >> >>> c.a[/color]
          >> Traceback (most recent call last):
          >> File "<stdin>", line 1, in ?
          >> AttributeError: 'C' object has no attribute 'a'[/color]
          >
          > So far so good, I think we agree;-).[/color]
          Yes.
          [color=blue][color=green][color=darkred]
          >> >>> class D(object):[/color]
          >> ... __dict__ = {}
          >> ...[color=darkred]
          >> >>> d = D()
          >> >>> d.a = 1
          >> >>> d.__dict__[/color]
          >> {}[color=darkred]
          >> >>> d.__dict__ = {}
          >> >>> d.a[/color]
          >> 1[/color]
          >
          > 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__'...[/color]

          Georg

          Comment

          • Alex Martelli

            #6
            Re: __dict__ strangeness

            Georg Brandl <g.brandl-nospam@gmx.net> wrote:
            [color=blue]
            > [moving to python-dev]
            >
            > Alex Martelli wrote:[color=green]
            > > Georg Brandl <g.brandl-nospam@gmx.net> wrote:
            > >[color=darkred]
            > >> can someone please tell me that this is correct and why:[/color]
            > >
            > > IMHO, it is not correct: it is a Python bug (and it would be nice to fix
            > > it in 2.5).[/color]
            >
            > Fine. Credits go to Michal Kwiatkowski for discovering that in bug #1448042
            > which I closed out of ignorance ;)[/color]

            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

            Comment

            • Ziga Seilnacht

              #7
              Re: __dict__ strangeness


              Georg Brandl wrote:[color=blue]
              > Hi,
              >
              > can someone please tell me that this is correct and why:
              >[color=green][color=darkred]
              > >>> class C(object):[/color][/color]
              > ... pass
              > ...[color=green][color=darkred]
              > >>> c = C()
              > >>> c.a = 1
              > >>> c.__dict__[/color][/color]
              > {'a': 1}[color=green][color=darkred]
              > >>> c.__dict__ = {}
              > >>> c.a[/color][/color]
              > Traceback (most recent call last):
              > File "<stdin>", line 1, in ?
              > AttributeError: 'C' object has no attribute 'a'[color=green][color=darkred]
              > >>>
              > >>> class D(object):[/color][/color]
              > ... __dict__ = {}
              > ...[color=green][color=darkred]
              > >>> d = D()
              > >>> d.a = 1
              > >>> d.__dict__[/color][/color]
              > {}[color=green][color=darkred]
              > >>> d.__dict__ = {}
              > >>> d.a[/color][/color]
              > 1
              >
              > Thanks,
              > Georg[/color]

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

              Ziga

              Comment

              • Alan Franzoni

                #8
                Re: __dict__ strangeness

                Georg Brandl on comp.lang.pytho n said:
                [color=blue]
                > It's 2.4.2, on Linux. The 2.5 SVN trunk has the same symptom.[/color]

                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

                Comment

                Working...