extending class

Steven D'Aprano steve+comp.lang.python at pearwood.info
Fri Sep 23 10:21:53 EDT 2011


Andrea Crotti wrote:

> I wanted to add a couple of parameters to a class from a given library
> (paste-script), but without changing the original code.
> So I thought, I create a wrapper class which adds what I need, and then
> dispatch all the calls to the super class.

You don't need to use a wrapper class if all you want is to add additional
attributes to an instance.

>>> class Spam(object):
...     def __init__(self):
...             self.x = 1
...
>>> s = Spam()
>>> s.name = "Fred"
>>> s.name
'Fred'


> My following attempt gives, however, a recursion error, but why?

Here is an old recipe showing how to do automatic delegation correctly:

http://code.activestate.com/recipes/52295/



-- 
Steven




More information about the Python-list mailing list