Re: Adding method from one class to another class or to instance of another class

2009-07-27 Thread 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Adding method from one class to another class or to instance of another class

2009-07-24 Thread Terry Reedy

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 "", line 1, in 
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

--
http://mail.python.org/mailman/listinfo/python-list


Re: Adding method from one class to another class or to instance of another class

2009-07-24 Thread Peter Otten
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 "", line 1, in 
> 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
> >
 b.createVars()
> 
> 
> 
> Then the following error raises:
> 
> 
> Traceback (most recent call last):
>   File "", line 1, in 
> 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


-- 
http://mail.python.org/mailman/listinfo/python-list


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

2009-07-24 Thread 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 "", line 1, in 
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
>
>>> b.createVars()



Then the following error raises:


Traceback (most recent call last):
  File "", line 1, in 
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
-- 
http://mail.python.org/mailman/listinfo/python-list