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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; '"""': 0.07; '%s"': 0.09; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'instance.': 0.09; 'message- id:@stoneleaf.us': 0.09; 'received:184.172': 0.09; 'received:gator410.hostgator.com': 0.09; 'val': 0.09; '~ethan~': 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:User-Agent:1': 0.23; 'header:In-Reply-To:1': 0.27; "doesn't": 0.30; 'object.': 0.31; 'class': 0.32; 'another': 0.32; 'skip:_ 10': 0.34; 'something': 0.35; 'charset:us-ascii': 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; 'new': 0.61; 'received:173': 0.61; 'name': 0.63; 'received:gateway15.websitewelcome.com': 0.84; 'subject:self': 0.84; 'subject:you': 0.87; 'do:': 0.91 Date: Tue, 09 Jul 2013 15:19:00 -0700 From: Ethan Furman User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121010 Thunderbird/16.0.1 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Recursive class | can you modify self directly? References: <61751485-a298-403e-8b44-be7cf2504f0e@googlegroups.com> In-Reply-To: <61751485-a298-403e-8b44-be7cf2504f0e@googlegroups.com> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - gator410.hostgator.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - stoneleaf.us X-BWhitelist: no X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: ([173.12.184.233]) [173.12.184.233]:59937 X-Source-Auth: ethan+stoneleaf.us X-Email-Count: 2 X-Source-Cap: dG9idWs7dG9idWs7Z2F0b3I0MTAuaG9zdGdhdG9yLmNvbQ== 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: 33 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1373410388 news.xs4all.nl 15871 [2001:888:2000:d::a6]:40339 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:50281 On 07/09/2013 03:01 PM, Russel Walker wrote: > > 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 name. In `expand()` you are rebinding the name `self` away from the object and to a new Expr instance. If you want to change `self` the original object you have to do something like: def expand(self): self.op = choice('+-*/') self.val = choice('12345') -- ~Ethan~