extending class

A

Andrea Crotti

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.

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

class PSIVar(object):
"""Extend var implementation from the paste-script, to add the
ability of correlating variablesTrue
"""
def __init__(self, first_var, other=None, fun=None):
# this is of type defined there
self.first_var = first_var
if other is not None:
self.other = other
self.fun = fun
assert callable(self.fun)

# now try to dispatch every method call to the other class
# must probably call the super class
def __getattribute__(self, attr):
return self.first_var.__getattribute__(attr)
 
S

Steven D'Aprano

Andrea said:
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.
.... def __init__(self):
.... self.x = 1
....
'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/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,076
Latest member
OrderKetoBeez

Latest Threads

Top