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: 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 Subject: Re: moving methods from class to instance of other class Date: Thu, 28 Jun 2012 13:57:25 -0400 References: 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: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 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