Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #24595

moving methods from class to instance of other class

From lars van gemerden <lars@rational-it.com>
Newsgroups comp.lang.python
Subject moving methods from class to instance of other class
Date 2012-06-27 23:59 -0700
Organization http://groups.google.com
Message-ID <c376d4cf-c888-4fa9-8d4c-032b80a43947@e20g2000vbm.googlegroups.com> (permalink)

Show all headers | View raw


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



Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

moving methods from class to instance of other class lars van gemerden <lars@rational-it.com> - 2012-06-27 23:59 -0700
  Re: moving methods from class to instance of other class Benjamin Kaplan <benjamin.kaplan@case.edu> - 2012-06-28 00:22 -0700
    Re: moving methods from class to instance of other class lars van gemerden <lars@rational-it.com> - 2012-06-28 01:14 -0700
  Re: moving methods from class to instance of other class Terry Reedy <tjreedy@udel.edu> - 2012-06-28 13:57 -0400
  Re: moving methods from class to instance of other class Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-06-28 14:15 -0400

csiph-web