Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!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; 'method.': 0.05; "'a'": 0.07; 'parameter': 0.07; '#error': 0.09; 'subclass': 0.09; 'stored': 0.10; '(the': 0.15; "'b'": 0.16; '(resulting': 0.16; '11:59': 0.16; 'a",': 0.16; 'a()': 0.16; 'a(object):': 0.16; 'bind': 0.16; 'classes:': 0.16; 'subject:class': 0.16; 'subject:instance': 0.16; 'unbind': 0.16; 'wed,': 0.16; 'wrote:': 0.17; 'all,': 0.21; 'cheers,': 0.23; 'tried': 0.25; 'header:In- Reply-To:1': 0.25; 'question': 0.27; 'message-id:@mail.gmail.com': 0.27; 'trouble': 0.28; 'prints': 0.29; 'question:': 0.29; 'subject:other': 0.29; 'skip:_ 10': 0.29; 'van': 0.29; 'class': 0.29; 'error': 0.30; 'stuff': 0.30; 'point': 0.31; 'could': 0.32; 'like:': 0.33; 'to:addr:python-list': 0.33; '(with': 0.33; "can't": 0.34; 'received:google.com': 0.34; 'self': 0.34; 'pm,': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'but': 0.36; 'method': 0.36; 'received:209': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'to:addr:python.org': 0.39 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding:x-gm-message-state; bh=F00gvVfgvEqwkSRqJtDF2Fa4UjYJ95KCgvf2iuVhIwU=; b=Pq+zCQxwfVPR9MBkasDZ7ZpJuhu5edwDLwRNhY9kbSbNMJgsGifAdDYXcLNh9CBn0V JS3665o9RPcRPbVdZWkCrowGIh5x+VnNgxFw5aAoBMwMOXpT3fDyZ7vCo5JLKenHcU1y AG7lVqvUsVLPqpxDq5VO8xNvLQ7+l+/RF169r7Yj7794kH7R/crHACNbe4uGqPgZr33R sFnaZi9Dsco6sHoL56+4SB27mfVFq1CfRZbbBWj4RwH6AZmjc1kO0RXUOlQBEq27OIFW AuriNQ8xRgMT9KIaAvj0wuYkKPwAB1wYwFE38BoodGFRzhs6wf+xWvKJDRVRhE/xmW9U fa7A== MIME-Version: 1.0 In-Reply-To: References: Date: Thu, 28 Jun 2012 00:22:25 -0700 Subject: Re: moving methods from class to instance of other class From: Benjamin Kaplan To: python-list@python.org Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Gm-Message-State: ALoCoQkQtoHGk08wihQ8SPV1idwlQVlIvLdZ24F1ZMvYrwBRQyOcm7NIJB5w0Ul8yqJAP0+o8UNC X-Junkmail-Whitelist: YES (by domain whitelist at mpv2.tis.cwru.edu) 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: 47 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1340868149 news.xs4all.nl 6940 [2001:888:2000:d::a6]:40049 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:24596 On Wed, Jun 27, 2012 at 11:59 PM, lars van gemerden wrote: > Hi all, > > I have some trouble with the following question: Let say i have the > following classes: > > class A(object): > =C2=A0 =C2=A0def __init__(self): > =C2=A0 =C2=A0 =C2=A0 =C2=A0self.name =3D 'a' > =C2=A0 =C2=A0def do(self): > =C2=A0 =C2=A0 =C2=A0 =C2=A0print 'A.do: self.name =3D', self.name > > class B(object): > =C2=A0 =C2=A0def __init__(self): > =C2=A0 =C2=A0 =C2=A0 =C2=A0self.name =3D 'b' > > > > The question is: How do i move the 'do' method from A to b (resulting > in =C2=A0printing "A.do: self.name =3D b")? > > I have tried (with a =3D A() and b =C2=A0B()): > > B.do =3D types.MethodType(A.do, b) #Error > > and stuff like: > > b.do =3D a.do > b.do() > > But either i get an error or b.do() prints =C2=A0"A.do: self.name =3D 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 =3D types.MethodType(A.do.im_func, b, B) but there's no point in re-inventing the wheel.