DictProxy? What is this?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • digitalorganics@gmail.com

    #1

    DictProxy? What is this?

    When I tried to update a class's __dict__, I got an error saying that
    there is no 'update' attribute for dictproxy object. What is a
    dictproxy object? I thought that __dict__ is always suppose to be a, no
    kidding, dictionary? Hence there should indeed be an update method.
    What's this proxy business?

    Thanks all.

  • Fredrik Lundh

    #2
    Re: DictProxy? What is this?

    digitalorganics @gmail.com wrote:
    [color=blue]
    > When I tried to update a class's __dict__, I got an error saying that
    > there is no 'update' attribute for dictproxy object. What is a
    > dictproxy object?[/color]

    a CPython implementation detail, used to protect an internal data structure used
    by new-style objects from unexpected modifications.

    </F>



    Comment

    • digitalorganics@gmail.com

      #3
      Re: DictProxy? What is this?


      Fredrik Lundh wrote:[color=blue]
      > digitalorganics @gmail.com wrote:
      >[color=green]
      > > When I tried to update a class's __dict__, I got an error saying that
      > > there is no 'update' attribute for dictproxy object. What is a
      > > dictproxy object?[/color]
      >
      > a CPython implementation detail, used to protect an internal data structure used
      > by new-style objects from unexpected modifications.
      >
      > </F>[/color]

      Ah, so I'm not suppose to be able to change a class's __dict__? Thanks
      Fredrik.

      Comment

      • Maric Michaud

        #4
        Re: DictProxy? What is this?

        Le lundi 26 juin 2006 16:06, digitalorganics @gmail.com a écrit :[color=blue]
        > Fredrik Lundh wrote:[color=green]
        > > digitalorganics @gmail.com wrote:[color=darkred]
        > > > When I tried to update a class's __dict__, I got an error saying that
        > > > there is no 'update' attribute for dictproxy object. What is a
        > > > dictproxy object?[/color]
        > >
        > > a CPython implementation detail, used to protect an internal data
        > > structure used by new-style objects from unexpected modifications.
        > >
        > > </F>[/color]
        >
        > Ah, so I'm not suppose to be able to change a class's __dict__? Thanks
        > Fredrik.[/color]

        Of course, yes :

        In [1]: class a(object) : pass
        ...:

        In [2]: a.__dict__
        Out[2]: <dictproxy object at 0xa7e01d34>

        In [3]: a.__dict__.item s()
        Out[3]:
        [('__dict__', <attribute '__dict__' of 'a' objects>),
        ('__module__', '__main__'),
        ('__weakref__', <attribute '__weakref__' of 'a' objects>),
        ('__doc__', None)]

        In [4]: a.__dict__['foo'] = lambda s : True
        ---------------------------------------------------------------------------
        exceptions.Type Error...

        TypeError: object does not support item assignment

        hmmm, not this way, but :

        In [5]: a.foo = lambda s : True

        In [6]: a.__dict__.item s()
        Out[6]:
        [('__dict__', <attribute '__dict__' of 'a' objects>),
        ('__module__', '__main__'),
        ('foo', <function <lambda> at 0xa79528ec>),
        ('__weakref__', <attribute '__weakref__' of 'a' objects>),
        ('__doc__', None)]

        In [7]: a().foo()
        Out[7]: True

        or :

        In [9]: setattr(a, 'bar', lambda s : False)

        In [10]: a().bar()
        Out[10]: False


        --
        _____________

        Maric Michaud
        _____________

        Aristote - www.aristote.info
        3 place des tapis
        69004 Lyon
        Tel: +33 426 880 097

        Comment

        • digitalorganics@gmail.com

          #5
          Re: DictProxy? What is this?

          All right. Thanks for explaining that Maric.

          Maric Michaud wrote:[color=blue]
          > Le lundi 26 juin 2006 16:06, digitalorganics @gmail.com a écrit :[color=green]
          > > Fredrik Lundh wrote:[color=darkred]
          > > > digitalorganics @gmail.com wrote:
          > > > > When I tried to update a class's __dict__, I got an error saying that
          > > > > there is no 'update' attribute for dictproxy object. What is a
          > > > > dictproxy object?
          > > >
          > > > a CPython implementation detail, used to protect an internal data
          > > > structure used by new-style objects from unexpected modifications.
          > > >
          > > > </F>[/color]
          > >
          > > Ah, so I'm not suppose to be able to change a class's __dict__? Thanks
          > > Fredrik.[/color]
          >
          > Of course, yes :
          >
          > In [1]: class a(object) : pass
          > ...:
          >
          > In [2]: a.__dict__
          > Out[2]: <dictproxy object at 0xa7e01d34>
          >
          > In [3]: a.__dict__.item s()
          > Out[3]:
          > [('__dict__', <attribute '__dict__' of 'a' objects>),
          > ('__module__', '__main__'),
          > ('__weakref__', <attribute '__weakref__' of 'a' objects>),
          > ('__doc__', None)]
          >
          > In [4]: a.__dict__['foo'] = lambda s : True
          > ---------------------------------------------------------------------------
          > exceptions.Type Error...
          >
          > TypeError: object does not support item assignment
          >
          > hmmm, not this way, but :
          >
          > In [5]: a.foo = lambda s : True
          >
          > In [6]: a.__dict__.item s()
          > Out[6]:
          > [('__dict__', <attribute '__dict__' of 'a' objects>),
          > ('__module__', '__main__'),
          > ('foo', <function <lambda> at 0xa79528ec>),
          > ('__weakref__', <attribute '__weakref__' of 'a' objects>),
          > ('__doc__', None)]
          >
          > In [7]: a().foo()
          > Out[7]: True
          >
          > or :
          >
          > In [9]: setattr(a, 'bar', lambda s : False)
          >
          > In [10]: a().bar()
          > Out[10]: False
          >
          >
          > --
          > _____________
          >
          > Maric Michaud
          > _____________
          >
          > Aristote - www.aristote.info
          > 3 place des tapis
          > 69004 Lyon
          > Tel: +33 426 880 097[/color]

          Comment

          Working...