moving methods from class to instance of other class

L

lars van gemerden

Hi all,

I have some trouble with the following question: Let say i have the
following classes:

class A(object):
def __init__(self):
self.name = 'a'
def do(self):
print 'A.do: self.name =', self.name

class B(object):
def __init__(self):
self.name = 'b'



The question is: How do i move the 'do' method from A to b (resulting
in printing "A.do: self.name = b")?

I have tried (with a = A() and b B()):

B.do = types.MethodType(A.do, b) #Error

and stuff like:

b.do = a.do
b.do()

But either i get an error or b.do() prints "A.do: self.name = a", so
the self parameter of a.do is stored somehow in the method.

In other words, how do i unbind 'do' from a/A and bind it to b (the
instance)?

Cheers, Lars
 
B

Benjamin Kaplan

Hi all,

I have some trouble with the following question: Let say i have the
following classes:

class A(object):
   def __init__(self):
       self.name = 'a'
   def do(self):
       print 'A.do: self.name =', self.name

class B(object):
   def __init__(self):
       self.name = 'b'



The question is: How do i move the 'do' method from A to b (resulting
in  printing "A.do: self.name = b")?

I have tried (with a = A() and b  B()):

B.do = types.MethodType(A.do, b) #Error

and stuff like:

b.do = a.do
b.do()

But either i get an error or b.do() prints  "A.do: self.name = a",so
the self parameter of a.do is stored somehow in the method.

In other words, how do i unbind 'do' from a/A and bind it to b (the
instance)?

Cheers, Lars

Is there any particular reason you can't just have B be a subclass of
A? You could do

b.do = types.MethodType(A.do.im_func, b, B)

but there's no point in re-inventing the wheel.
 
L

lars van gemerden

Is there any particular reason you can't just have B be a subclass of
A? You could do

b.do = types.MethodType(A.do.im_func, b, B)

but there's no point in re-inventing the wheel.

Perfect, Thank you,

As to the why, to make a long story short, actually instantiation
would fit better conceptually than inheritance in this case, but that
would mean the 'A' instances would be types, which introduces
metaclasses, which i tried but i ran into problems with e.g. pickle
and with complexity.

Cheers, Lars
 
T

Terry Reedy

class A(object):
def __init__(self):
self.name = 'a'
def do(self):
print 'A.do: self.name =', self.name

class B(object):
def __init__(self):
self.name = 'b'

The question is: How do i move the 'do' method from A to b
(resulting in printing "A.do: self.name = b")?

If you want to move the method from class A to class B
(which is normally more sensible than to instance b of B)

B.do = A.do.im_func # Python 2
B.do = A.do # Python 3
b = B()
b.do()
# print (with print adjusted for PY3)
A.do: self.name = b

If you want a B instance to act like an A instance, you can change its
class (subject to some limitations). The following works.

b = B()
b.__class__ = A
b.do()

If make the change temporary and the reversion automatic, write a
context manager.
 
D

Dennis Lee Bieber

Hi all,

I have some trouble with the following question: Let say i have the
following classes:

class A(object):
def __init__(self):
self.name = 'a'
def do(self):
print 'A.do: self.name =', self.name

class B(object):
def __init__(self):
self.name = 'b'



The question is: How do i move the 'do' method from A to b (resulting
in printing "A.do: self.name = b")?
.... def __init__(self):
.... self.name = "a"
.... def do(self):
.... print "A.do: self.name = %s" % self.name
........ def __init__(self):
.... super(B, self).__init__()
.... self.name = "b"
....
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top