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


Groups > comp.lang.python > #50399

Re: Recursive class | can you modify self directly?

References <61751485-a298-403e-8b44-be7cf2504f0e@googlegroups.com> <ba392048-0140-4781-9079-f85531ea41de@googlegroups.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date 2013-07-10 17:39 -0600
Subject Re: Recursive class | can you modify self directly?
Newsgroups comp.lang.python
Message-ID <mailman.4554.1373499593.3114.python-list@python.org> (permalink)

Show all headers | View raw


On Wed, Jul 10, 2013 at 4:50 PM, Russel Walker <russ.pobox@gmail.com> 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

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Recursive class | can you modify self directly? Russel Walker <russ.pobox@gmail.com> - 2013-07-09 15:01 -0700
  Re: Recursive class | can you modify self directly? Ian Kelly <ian.g.kelly@gmail.com> - 2013-07-09 16:18 -0600
  Re: Recursive class | can you modify self directly? Ian Kelly <ian.g.kelly@gmail.com> - 2013-07-09 16:20 -0600
    Re: Recursive class | can you modify self directly? Russel Walker <russ.pobox@gmail.com> - 2013-07-10 02:00 -0700
  Re: Recursive class | can you modify self directly? Dave Angel <davea@davea.name> - 2013-07-09 18:30 -0400
  Re: Recursive class | can you modify self directly? Ethan Furman <ethan@stoneleaf.us> - 2013-07-09 15:19 -0700
  Re: Recursive class | can you modify self directly? Russel Walker <russ.pobox@gmail.com> - 2013-07-10 01:58 -0700
    Re: Recursive class | can you modify self directly? Terry Reedy <tjreedy@udel.edu> - 2013-07-10 15:33 -0400
      Re: Recursive class | can you modify self directly? Russel Walker <russ.pobox@gmail.com> - 2013-07-10 13:09 -0700
  Re: Recursive class | can you modify self directly? Russel Walker <russ.pobox@gmail.com> - 2013-07-10 15:50 -0700
    Re: Recursive class | can you modify self directly? Ian Kelly <ian.g.kelly@gmail.com> - 2013-07-10 17:39 -0600

csiph-web