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


Groups > comp.lang.python > #24629

Re: moving methods from class to instance of other class

Path csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'context': 0.05; "'a'": 0.07; 'works.': 0.07; 'python': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'terry': 0.09; 'def': 0.10; "'b'": 0.16; '(resulting': 0.16; 'a(object):': 0.16; 'adjusted': 0.16; 'b()': 0.16; 'message-id:@dough.gmane.org': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'subject:class': 0.16; 'subject:instance': 0.16; 'wrote:': 0.17; 'instance': 0.17; 'instance,': 0.17; 'jan': 0.18; 'header:In- Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; '(which': 0.26; 'am,': 0.27; 'question': 0.27; 'header:X-Complaints-To:1': 0.28; 'sensible': 0.29; 'subject:other': 0.29; 'skip:_ 10': 0.29; 'van': 0.29; 'class': 0.29; 'normally': 0.30; 'print': 0.32; 'to:addr :python-list': 0.33; '(with': 0.33; 'received:org': 0.36; 'method': 0.36; 'subject:: ': 0.38; 'some': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'more': 0.63; 'received:fios.verizon.net': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: moving methods from class to instance of other class
Date Thu, 28 Jun 2012 13:57:25 -0400
References <c376d4cf-c888-4fa9-8d4c-032b80a43947@e20g2000vbm.googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host pool-74-109-121-73.phlapa.fios.verizon.net
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:13.0) Gecko/20120614 Thunderbird/13.0.1
In-Reply-To <c376d4cf-c888-4fa9-8d4c-032b80a43947@e20g2000vbm.googlegroups.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1616.1340906279.4697.python-list@python.org> (permalink)
Lines 40
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1340906280 news.xs4all.nl 6915 [2001:888:2000:d::a6]:33962
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:24629

Show key headers only | View raw


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


Back to comp.lang.python | Previous | NextPrevious in thread | Next 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