Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!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.023 X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; '"""': 0.07; 'modify': 0.07; '%s"': 0.09; 'bindings': 0.09; 'title.': 0.09; 'val': 0.09; 'variable,': 0.09; 'def': 0.12; 'random': 0.14; 'expression.': 0.16; 'simplified': 0.16; 'subject:class': 0.16; 'wrote:': 0.18; 'example': 0.22; 'import': 0.22; 'header:In-Reply-To:1': 0.27; "doesn't": 0.30; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'object.': 0.31; 'class': 0.32; 'probably': 0.32; 'another': 0.32; 'skip:_ 10': 0.34; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'explains': 0.36; 'object,': 0.36; 'subject:?': 0.36; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'subject:can': 0.39; 'to:addr:python.org': 0.39; 'expression': 0.60; 'show': 0.63; 'name': 0.63; 'jul': 0.74; 'affected.': 0.84; 'subject:self': 0.84; 'subject:you': 0.87; 'do:': 0.91; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=qguFV7QvgGNN5dxN1nJ3a8C4XHe02U/qCMZF1Gm7fls=; b=DIlJtMJtw945roHuSMJ7NlGv4iNIvmJsulD99F8rxPo4KeSALyoeiYgkBlZI2VgKgr S6cGp8Z4EY5WsZRzf33455kGgLfrmZXbQVF/uBnInvKlxSt4a8vKm/8qk7VLLTIH30v9 wDdhK5Qz9BdUzbxKELKWbdK6f5I4revFPveRlZ1kNnrQFGodjcs3dPj1TotHQqQxMoZO S1GoK0DGm+VIeu7b28yjjd1WS/J7/gQPNEBDbZn4N9t9tWY2fNR5t1T5a/1T6wCPh1gj EJvVgjuUt08mSZAW4neyIspFQTwUo3Ee7h8Lzdd9WIXqGdrffI69wl8m3WPFUDxuAgOI 62Uw== X-Received: by 10.68.217.7 with SMTP id ou7mr28558017pbc.8.1373408321990; Tue, 09 Jul 2013 15:18:41 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <61751485-a298-403e-8b44-be7cf2504f0e@googlegroups.com> References: <61751485-a298-403e-8b44-be7cf2504f0e@googlegroups.com> From: Ian Kelly Date: Tue, 9 Jul 2013 16:18:00 -0600 Subject: Re: Recursive class | can you modify self directly? To: Python Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 38 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1373408331 news.xs4all.nl 16000 [2001:888:2000:d::a6]:59040 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:50278 On Tue, Jul 9, 2013 at 4:01 PM, Russel Walker wrote: > Sorry for the vague title. Probably best to just show you the code that explains it better. > > This is a simplified example of what I want to do: > > > # THIS DOESN'T WORK > from random import choice > > class Expr(object): > """ > Expr(expr, op, val) -> an expression object. > """ > > def __init__(self, expr, op='', val=''): > self.expr = expr # can be another instance of Expression. > self.op = op > self.val = val > > def __str__(self): > return ("%s %s %s" % (self.expr, self.op, self.val)).strip() > > def expand(self): > self = Expr(self, choice('+-*/'), choice('12345')) "self" is just a local binding of the object to the name self. You can rebind the name like this as with any other local variable, but that's all it does. It doesn't modify the object in any way, and no other bindings of the same object are affected. If you actually want to modify the current object, you would need to do something like: def expand(self): import copy self.expr = Expr(self.expr, self.op, self.val) self.op = choice('+-*/') self.val = choice('12345')