Classic vs New-style classes...

Martin Chilvers martin at enthought.com
Fri Jun 20 18:17:25 EDT 2003


I've searched the archives and docs for an answer to this one, but I'm
still stuck...

I am modifying some existing "Classic-class" code that dynamically
attaches a method to a class.

E.g.

>>> class Foo:
...     pass
...
>>> Foo.__dict__['bar'] = lambda self, x: x * 2
>>> type(Foo.__dict__)
<type 'dict'>
>>>

Now, I want to change Foo to be a "New-style" class (so that I can give
it a meta-class) and the problem is that I can't seem to modify it's
class dictionary.

E.g.

>>> class Foo(object)
...	pass
...
>>> Foo.__dict__['bar'] = lambda self, x: x * 2
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: object does not support item assignment
>>> type(Foo.__dict__)
<type 'dict-proxy'>

Any ideas?

Martin Chilvers
martin at enthought.com
"There's no such thing as free software."







More information about the Python-list mailing list