Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; '"""': 0.07; 'assignment': 0.07; '%s"': 0.09; 'indicates': 0.09; 'messing': 0.09; 'operator,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'title.': 0.09; 'val': 0.09; 'def': 0.12; 'itself.': 0.14; 'random': 0.14; 'expression.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'simplified': 0.16; 'subject:class': 0.16; 'wrote:': 0.18; 'example': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; "i've": 0.25; 'class.': 0.26; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; 'idea': 0.28; 'expanding': 0.29; "doesn't": 0.30; 'work.': 0.31; 'code': 0.31; 'changed.': 0.31; 'object.': 0.31; 'class': 0.32; 'probably': 0.32; 'supposed': 0.32; 'another': 0.32; 'totally': 0.33; 'skip:_ 10': 0.34; 'something': 0.35; 'objects': 0.35; 'but': 0.35; 'really': 0.36; 'explains': 0.36; 'interact': 0.36; 'object,': 0.36; 'doing': 0.36; "didn't": 0.36; 'method': 0.36; 'responsible': 0.36; 'subject:?': 0.36; 'work?': 0.38; 'to:addr :python-list': 0.38; 'fact': 0.38; 'pm,': 0.38; 'does': 0.39; 'itself': 0.39; 'subject:can': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'expression': 0.60; 'new': 0.61; 'show': 0.63; 'name': 0.63; 'different': 0.65; 'to,': 0.72; 'subject:self': 0.84; 'wanted,': 0.84; 'subject:you': 0.87; 'do:': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Dave Angel Subject: Re: Recursive class | can you modify self directly? Date: Tue, 09 Jul 2013 18:30:29 -0400 References: <61751485-a298-403e-8b44-be7cf2504f0e@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: 174.32.174.33 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130623 Thunderbird/17.0.7 In-Reply-To: <61751485-a298-403e-8b44-be7cf2504f0e@googlegroups.com> 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: 63 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1373409051 news.xs4all.nl 15867 [2001:888:2000:d::a6]:36888 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:50280 On 07/09/2013 06: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')) > > > Then I tried messing around with Expr.__new__() and Expr.__init__() but that didn't work. > > The only way I've got it to work is by doing the expanding part outside of the object, by a method of some other class. But I want the Expr class to be responsible for itself. How can I do this and why doesn't the above work? > That line in method expand() indicates that you have a fundamental misunderstanding of the way that names and objects interact in Python. Names are bound to objects by the assignment operator, but the object itself is not changed. If you want to change the object that self is bound to, then change it by mutating it. You don't change an existing object by binding a name to a new object, whether of the same or different type. The original object remains unchanged. The fact that the name is 'self' is totally irrelevant. a = 45 b = a a = 12 This does NOT change b. No idea what you really wanted, but perhaps it's something like: def expand(self): self.op = choice("+-*/") self.val = choice("12345") No idea what expr is supposed to represent, so I didn't try to manipulate it here. -- DaveA