changing new style classes py2.2

Robert Kuzelj robert_kuzelj at yahoo.com
Wed May 29 16:25:22 EDT 2002


hi,

i am having some problems using new style classes.
i want to build something like an class-enhancer
so i need write-access to the class.__dict__.
but this seems to be forbidden now as opposed
to oldsytle classes.

>>> #some examples
>>class A(object):
>>..def meth1(self): pass
>>..def meth2(self): pass

>> A.__dict__["meth3"] = A.meth1
this throws "TypeError: object does not support item assignment"
this was perfectly legal in old style classes.

on the other hand this is still legal (that means
it still works without error).
>>A.meth3 = A.meth1

so i could at least workaround by writing the following code
>>exec "A.%(name1)s = A.%(name2)s" % {"name1": "meth5", "name2": "meth1"}
<booh> not very clear or very pythonic.

besides the fact, that this behaviour seems to be inconsistent
with the old-style it is inconsistent with it self. either one
can change a class (then it should be possible by writing to
the __dict__) or one can not.

the workaraound is simply ugly (and sure is slower).

ciao robertj



More information about the Python-list mailing list