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?
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to