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


Groups > comp.lang.python > #24595 > unrolled thread

moving methods from class to instance of other class

Started bylars van gemerden <lars@rational-it.com>
First post2012-06-27 23:59 -0700
Last post2012-06-28 14:15 -0400
Articles 5 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  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

#24595 — moving methods from class to instance of other class

Fromlars van gemerden <lars@rational-it.com>
Date2012-06-27 23:59 -0700
Subjectmoving methods from class to instance of other class
Message-ID<c376d4cf-c888-4fa9-8d4c-032b80a43947@e20g2000vbm.googlegroups.com>
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



[toc] | [next] | [standalone]


#24596

FromBenjamin Kaplan <benjamin.kaplan@case.edu>
Date2012-06-28 00:22 -0700
Message-ID<mailman.1590.1340868149.4697.python-list@python.org>
In reply to#24595
On Wed, Jun 27, 2012 at 11:59 PM, lars van gemerden
<lars@rational-it.com> wrote:
> 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.

[toc] | [prev] | [next] | [standalone]


#24598

Fromlars van gemerden <lars@rational-it.com>
Date2012-06-28 01:14 -0700
Message-ID<4dbb6034-cad2-4605-adf6-ad6a03ad82f2@o4g2000yqk.googlegroups.com>
In reply to#24596
On Jun 28, 9:22 am, Benjamin Kaplan <benjamin.kap...@case.edu> wrote:
> On Wed, Jun 27, 2012 at 11:59 PM, lars van gemerden
>
>
>
>
>
>
>
>
>
> <l...@rational-it.com> wrote:
> > 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.

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

[toc] | [prev] | [next] | [standalone]


#24629

FromTerry Reedy <tjreedy@udel.edu>
Date2012-06-28 13:57 -0400
Message-ID<mailman.1616.1340906279.4697.python-list@python.org>
In reply to#24595
On 6/28/2012 2:59 AM, lars van gemerden wrote:

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

-- 
Terry Jan Reedy


[toc] | [prev] | [next] | [standalone]


#24630

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2012-06-28 14:15 -0400
Message-ID<mailman.1617.1340907329.4697.python-list@python.org>
In reply to#24595
On Wed, 27 Jun 2012 23:59:22 -0700 (PDT), lars van gemerden
<lars@rational-it.com> declaimed the following in
gmane.comp.python.general:

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

>>> class A(object):
... 	def __init__(self):
... 		self.name = "a"
... 	def do(self):
... 		print "A.do: self.name = %s" % self.name
... 		
>>> class B(A):
... 	def __init__(self):
... 		super(B, self).__init__()
... 		self.name = "b"
... 		
>>> anA = A()
>>> aB = B()
>>> anA.do()
A.do: self.name = a
>>> aB.do()
A.do: self.name = b
>>> 
-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
        wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web