Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Adding method from one class to another class or to instance ofanother class

Reply
Thread Tools

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

 
 
marekw2143
Guest
Posts: n/a
 
      07-24-2009
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:

>>> b=B()
>>> b.createVars = new.instancemethod(A.createVars, b, B)
>>> b.createVars

<bound method B.createVars of <__main__.B object at 0x7f6330cc4a90>>
>>> b.createVars()




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
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      07-24-2009
marekw2143 wrote:

> 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:
>
>>>> b=B()
>>>> b.createVars = new.instancemethod(A.createVars, b, B)
>>>> b.createVars

> <bound method B.createVars of <__main__.B object at 0x7f6330cc4a90>>
>>>> b.createVars()

>
>
>
> 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?


>>> class A(object):

.... def create_vars(self):
.... self.x = 42
....
>>> class B(object): pass

....
>>> B.create_vars = A.create_vars.im_func
>>> b = B()
>>> b.create_vars()
>>> b.x

42

An alternative I find a bit cleaner:

>>> def create_vars(self): self.x = 42

....
>>> class A(object):

.... create_vars = create_vars
....
>>> class B(object):

.... create_vars = create_vars
....
>>> b = B()
>>> b.create_vars()
>>> b.x

42

Peter


 
Reply With Quote
 
 
 
 
Terry Reedy
Guest
Posts: n/a
 
      07-24-2009
marekw2143 wrote:
> 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

 
Reply With Quote
 
marekw2143
Guest
Posts: n/a
 
      07-25-2009
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
injecting an instance method into another class' class method Chuck Remes Ruby 3 08-12-2010 07:15 PM
why can't an instance instantiated within a class method access aprotected instance method? Greg Hauptmann Ruby 9 06-16-2008 10:16 AM
instance method adding another instance method to the class Raj Singh Ruby 2 05-29-2008 10:09 PM
[Q] Making functional called in one thread run in the context ofanother? spam@spam.com Java 11 10-18-2007 07:27 PM
request.isAuthenticated becomes false after redirect to a URL ofanother asp.net project? Nick ASP .Net 0 08-12-2003 01:16 AM



Advertisments