Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
moving methods from class to instance of other class
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  4 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
lars van gemerden  
View profile  
 More options Jun 28 2012, 2:59 am
Newsgroups: comp.lang.python
From: lars van gemerden <l...@rational-it.com>
Date: Wed, 27 Jun 2012 23:59:22 -0700 (PDT)
Local: Thurs, Jun 28 2012 2:59 am
Subject: moving methods from class to instance of other class
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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Benjamin Kaplan  
View profile  
 More options Jun 28 2012, 3:22 am
Newsgroups: comp.lang.python
From: Benjamin Kaplan <benjamin.kap...@case.edu>
Date: Thu, 28 Jun 2012 00:22:25 -0700
Local: Thurs, Jun 28 2012 3:22 am
Subject: Re: moving methods from class to instance of other class
On Wed, Jun 27, 2012 at 11:59 PM, 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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
lars van gemerden  
View profile  
 More options Jun 28 2012, 4:14 am
Newsgroups: comp.lang.python
From: lars van gemerden <l...@rational-it.com>
Date: Thu, 28 Jun 2012 01:14:23 -0700 (PDT)
Local: Thurs, Jun 28 2012 4:14 am
Subject: Re: moving methods from class to instance of other class
On Jun 28, 9:22 am, Benjamin Kaplan <benjamin.kap...@case.edu> wrote:

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Terry Reedy  
View profile  
 More options Jun 28 2012, 1:57 pm
Newsgroups: comp.lang.python
From: Terry Reedy <tjre...@udel.edu>
Date: Thu, 28 Jun 2012 13:57:25 -0400
Local: Thurs, Jun 28 2012 1:57 pm
Subject: Re: moving methods from class to instance of other class
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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »