Path: csiph.com!usenet.pasdenom.info!news.albasani.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed2.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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'operator': 0.03; 'tree': 0.05; 'assignment': 0.07; '__init__': 0.09; 'appropriate.': 0.09; 'caller': 0.09; 'function:': 0.09; 'oop': 0.09; 'structure,': 0.09; 'def': 0.12; '"in"': 0.16; "%s)'": 0.16; "'+'": 0.16; '2):': 0.16; 'contents:': 0.16; 'distinct': 0.16; 'expression,': 0.16; 'itself,': 0.16; 'len(self)': 0.16; 'overridden': 0.16; 'subject:class': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'seems': 0.21; '>>>': 0.22; 'input': 0.22; 'print': 0.22; 'builder': 0.24; 'finally,': 0.24; 'replace': 0.24; "shouldn't": 0.24; 'this:': 0.26; 'header:In-Reply-To:1': 0.27; 'said,': 0.30; 'message-id:@mail.gmail.com': 0.30; 'work.': 0.31; 'container': 0.31; 'allows': 0.31; 'class': 0.32; 'lists': 0.32; 'probably': 0.32; 'cases': 0.33; 'continuing': 0.33; 'actual': 0.34; 'skip:_ 10': 0.34; 'could': 0.34; 'problem': 0.35; 'something': 0.35; 'usual': 0.35; 'test': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'false': 0.36; 'doing': 0.36; 'subject:?': 0.36; 'should': 0.36; 'example,': 0.37; 'two': 0.37; 'list': 0.37; 'list.': 0.37; 'easily': 0.37; 'expected': 0.38; 'implement': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'expect': 0.39; 'does': 0.39; 'subject:can': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'expression': 0.60; 'helps': 0.61; 'length': 0.61; 'course': 0.61; "you're": 0.61; 'subject': 0.69; 'jul': 0.74; 'behavior': 0.77; 'self.value': 0.84; 'slicing?': 0.84; 'subject:self': 0.84; 'x):': 0.84; 'subject:you': 0.87; '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=Et7u+e4xoh4HXtoYBAssRUJtzxau7bojS5EpqFCZJxw=; b=iWcO5D/FbATGuujnap7WQuoVObMrNok/QlZ77zxcTFxALCthAznmA9adx7kY5WoFKX 3yBQhOD8llzKUHCflEXzWjguQNNBfzfSynIK9poLKK5BoT/E3xDG+3m3+EGiZZozZUoF 2DErABqfyYHKvYn/GMqdteoKABACWKinmq1sArSPeEYW43bL4BXwZwiF/8PeMy37L7oV g7eCWV0OztDRM93yWNTZ1HHqlZqTKXqeBhxnZINcc0wTjBTjEqjwE44OnY9DqDAP0wjY AFUqRrvbeg+l2GvZg/2saeTSeXxGuYw70wtT3iwn1UO5sl/xdh+ti7pRCVWwV4e9MGQV OqlQ== X-Received: by 10.66.249.202 with SMTP id yw10mr35141967pac.145.1373499584346; Wed, 10 Jul 2013 16:39:44 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: <61751485-a298-403e-8b44-be7cf2504f0e@googlegroups.com> From: Ian Kelly Date: Wed, 10 Jul 2013 17:39:04 -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: 88 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1373499593 news.xs4all.nl 15919 [2001:888:2000:d::a6]:36756 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:50399 On Wed, Jul 10, 2013 at 4:50 PM, Russel Walker wrote: > def append(self, x): > if len(self) < 3: > list.append(self, x) > else: > oldself = LRExpression(*self) > self.__init__(oldself) > self.append(x) It's probably not wise to be re-calling __init__ after the class has been initialized, although it seems to work. I would instead use slice assignment to replace the list contents: self[:] = [oldself] That said, the problem with inheriting from list is that it allows your class to be used in a lot of ways lists can be used that are probably not appropriate. For example, continuing on from your test function: >>> len(a) 3 Why 3? Because that's the length of the list. But the caller would probably expect the length to have something to do with the size of the expression, not the list. >>> '*' in a # Is the '*' operator used in a? True >>> '+' in a # What about the '+' operator? False Again, this happens because the "in" operator is only looking at the list itself, not at sublists. But an expression container would probably be expected to return True for both of those. These cases can of course be easily overridden in the subclass, but what about something harder, like slicing? How should that be expected to work with an LRExpression? This isn't to say that you shouldn't necessarily use a list if it helps you implement the behavior you want, but having the LRExpression *be* a list is probably wrong. Finally, based on what you're doing here, I think you would be better off using something like the OOP Builder pattern. There will be two distinct classes: the "LRExpression" class that composes a tree structure, and an "ExpressionBuilder" class that does the work of actual assembly. It could look something like this: class LRExpression(object): def __init__(self, expr, op, value): self.expr = expr self.op = op self.value = value def __str__(self): return '(%s %s %s)' % (self.expr, self.op, self.value) def evaluate(self): # Subject to the usual warning that eval() should # never be used with untrusted input return eval(str(self)) class ExpressionBuilder(object): def __init__(self): self.parts = [] def append(self, part): self.parts.append(part) def build(self): expr = self.parts[0] for i in xrange(1, len(self.parts), 2): op, value = self.parts[i:i+2] expr = LRExpression(expr, op, value) return expr def test(): a = ExpressionBuilder() a.append(1) a.append('+') a.append(2) a.append('*') a.append(3) expr = a.build() print expr print expr.evaluate() >>> test() ((1 + 2) * 3) 9