Bug or wart? You make the call.

Terry Reedy tjreedy at udel.edu
Sat Mar 8 12:20:10 EST 2003


"Alex A. Naanou" <alex_nanou at pochtamt.ru> wrote in message
news:def34119.0303080733.11b3a615 at posting.google.com...
> Why not proxy attribute creation to the wrapped object?
>   I do find the current state strange and inconsistent...

I partly agree.

> class X(object):
>     def meth(self):pass
>
> x = X()
> x.__dict__['xx'].attr0 = 0
> X.__dict__['xx'].attr1 = 1

Huh?  This gives attribute errors.  Perhaps you meant 'meth' instead
of 'xx'?  However
x.__dict__['meth'].attr0 = 0 # still does not work, while
X.__dict__['meth'].attr1 = 1 # does, so what are you really saying?
The method is in the class dict only, not the instance dict.

> # another way that works:
> x.meth.__setsttr__('attr2', 2)
> X.meth.__setsttr__('attr3', 3)

No.  You of course meant setattr; the following do work (somewhat to
my surprise):
x.meth.__setattr__('attr2', 2)
X.meth.__setattr__('attr3', 3)

> print x.meth.attr0, x.meth.attr1, x.meth.attr2, x.meth.attr3
> # will print "0 1 2 3"

No it will not, at least not in 2.2.1.
>>> print x.meth.attr0, x.meth.attr1, x.meth.attr2, x.meth.attr3
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: 'function' object has no attribute 'attr0'

In 8 lines of code, 5 are erroneous.  Use the interactive interpreter.
Cut and paste *actual* (not made-up)  results like experienced posters
do:
>>> print x.meth.attr1, x.meth.attr2, x.meth.attr3
1 2 3
It saves embarrassment.

> # so why not make this work as well:
> ## x.meth.attr4 = 4

Suggesting that x.meth.attr=v act like x.meth.__settattr__('attr',v)
is plausibly sensible.  I suspect the current difference may have
something to do with the way magic methods are handled for new-style
classes.  Whether this difference is planned or an unplanned
side-effect I do not know.

Terry J. Reedy










More information about the Python-list mailing list