Adding method from one class to another class or to instance ofanother class

M

marekw2143

Hi,

I have one class (A) that has defined method createVars. I would like
to add that method to class B
The code looks like this:


class A(object):
def createVars(self):
self.v1 = 1
self.v2 = 3
pass

class B(object):
pass


I don't want to use inheritance (because class A has many methods
defined that class B doesn't need).
When I try the folloowing:


B.createVars = C.createVars
B().createVars()


then the following error occurs:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unbound method createVars() must be called with A instance
as first argument (got nothing instead)

When I try to add the createVars method to instance of B:



Then the following error raises:


Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unbound method createVars() must be called with A instance
as first argument (got B instance instead)



How can I solve this problem?

Regards,
Marek
 
P

Peter Otten

marekw2143 said:
Hi,

I have one class (A) that has defined method createVars. I would like
to add that method to class B
The code looks like this:


class A(object):
def createVars(self):
self.v1 = 1
self.v2 = 3
pass

class B(object):
pass


I don't want to use inheritance (because class A has many methods
defined that class B doesn't need).

You can move createVars() into a mixin or common base class:

class M(object):
def createVars(self): ...

class A(M):
...

class B(M)
...
When I try the folloowing:


B.createVars = C.createVars
B().createVars()


then the following error occurs:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unbound method createVars() must be called with A instance
as first argument (got nothing instead)

When I try to add the createVars method to instance of B:




Then the following error raises:


Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unbound method createVars() must be called with A instance
as first argument (got B instance instead)



How can I solve this problem?
.... def create_vars(self):
.... self.x = 42
....42

An alternative I find a bit cleaner:
.... create_vars = create_vars
........ create_vars = create_vars
....42

Peter
 
T

Terry Reedy

marekw2143 said:
Hi,

I have one class (A) that has defined method createVars. I would like
to add that method to class B
The code looks like this:


class A(object):
def createVars(self):
self.v1 = 1
self.v2 = 3
pass

class B(object):
pass


I don't want to use inheritance (because class A has many methods
defined that class B doesn't need).
When I try the folloowing:


B.createVars = C.createVars

you meant A.createVars
B().createVars()


then the following error occurs:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unbound method createVars() must be called with A instance
as first argument (got nothing instead)

In 3.1, your example works fine. The difference is that in 2.x,
B.createVars is a method wrapperthat wraps the function, whereas in 3.1,
it is the function itself. For 2.x, you need to extract the function
from the wrapper. It is im_func or something like that. Use
dir(B.createVars) to check for sure.
How can I solve this problem?

Terry Jan Reedy
 
M

marekw2143

Thanks for your responses. im_func is all I need. I considered
subclassing, wchih is more easy to extend, but I needed some quick way
to add a method to another class.

Regards,
Marek
 

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,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top