Yes, for general objects, you have to override __new__. In general,
you just need to define a __new__ that looks like the one above,
except without the "if evaluate" branch (if you don't care about
noncommutatives you can skip that bit too).  Basically

- sympify the arguments. _sympify is used so that strings aren't supported.
- Create a new Expr of the given class. If your class doesn't subclass
from Expr, use Basic instead. This contains some vital logic such as
setting up the hash, so don't skip it.
- Return that object.

By the way, for your Add and Mul, in your flatten, you may want to
override flatten to actually flatten things, so that NoevalAdd(x, y,
NoevalAdd(z, t)) is the same as NoevalAdd(x, y, z, t). Otherwise,
there's little point in overriding flatten instead of __new__ there.

Aaron Meurer


On Fri, Oct 11, 2013 at 8:21 AM, Ben Lucato <[email protected]> wrote:
> This is awesome!!! Your simple fix with overriding flatten() works
> perfectly. My last question is -- how do you do the same thing with Pow? It
> is subclassed differently to Add, Mul (which both subclass "AssocOp").
> I have taken a look at the source code (go me!!!) - it seems that in this
> case, because Pow is defined like this:
>
> "def __new__(cls, b, e, evaluate=True):
> b = _sympify(b) e = _sympify(e) if evaluate: if e is S.Zero: return S.One
> elif e is S.One: return b else: obj = b._eval_power(e) if obj is not None:
> return obj obj = Expr.__new__(cls, b, e) obj.is_commutative =
> (b.is_commutative and e.is_commutative) return obj"
>
>
> So it seems a change to __new__ is necessary, right? It looks like
> evaluate=False is not even supported for Pow - am I right? (just a noob
> asking questions)
>
> Thanks!!
>
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/sympy.
> For more options, visit https://groups.google.com/groups/opt_out.

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sympy.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to