DictProxy? What is this?

digitalorganics at gmail.com digitalorganics at gmail.com
Mon Jun 26 10:28:26 EDT 2006


All right. Thanks for explaining that Maric.

Maric Michaud wrote:
> Le lundi 26 juin 2006 16:06, digitalorganics at gmail.com a écrit :
> > Fredrik Lundh wrote:
> > > digitalorganics at 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>
> >
> > Ah, so I'm not suppose to be able to change a class's __dict__? Thanks
> > Fredrik.
>
> Of course, yes :
>
> In [1]: class a(object) : pass
>    ...:
>
> In [2]: a.__dict__
> Out[2]: <dictproxy object at 0xa7e01d34>
>
> In [3]: a.__dict__.items()
> 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.TypeError...
>
> TypeError: object does not support item assignment
>
> hmmm, not this way, but :
>
> In [5]: a.foo = lambda s : True
>
> In [6]: a.__dict__.items()
> 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




More information about the Python-list mailing list