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


Groups > comp.lang.python > #24596

Re: moving methods from class to instance of other class

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 <bsk16@case.edu>
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 <c376d4cf-c888-4fa9-8d4c-032b80a43947@e20g2000vbm.googlegroups.com>
References <c376d4cf-c888-4fa9-8d4c-032b80a43947@e20g2000vbm.googlegroups.com>
Date Thu, 28 Jun 2012 00:22:25 -0700
Subject Re: moving methods from class to instance of other class
From Benjamin Kaplan <benjamin.kaplan@case.edu>
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 <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.1590.1340868149.4697.python-list@python.org> (permalink)
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

Show key headers only | View raw


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.

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