On Dec 28, 2007 11:48 AM, Pearu Peterson <[EMAIL PROTECTED]> wrote: > > > > On Dec 28, 10:12 am, "Ondrej Certik" <[EMAIL PROTECTED]> wrote: > > On Dec 28, 2007 10:18 AM, Pearu Peterson <[EMAIL PROTECTED]> wrote: > > > > > > > > Try deriving Foo from sympy.Basic. Actually the __mul__ code in sympy > > > is broken as it should return NotImplemented when it gets an > > > unknown (to sympy) object as an right hand side. > > > > It raises: > > > > Traceback (most recent call last): > > File "t.py", line 21, in ? > > y*f # does not work > > File "/home/ondra/sympy/sympy/core/methods.py", line 32, in __mul__ > > return Basic.Mul(self, other) > > File "sympy/core/basic_methods.py", line 122, in wrapper > > func_cache_it_cache[k] = r = func(*args, **kw_args) > > File "/home/ondra/sympy/sympy/core/operations.py", line 19, in __new__ > > c_part, nc_part, lambda_args, order_symbols = > > cls.flatten(map(Basic.sympify, args)) > > File "sympy/core/basic.py", line 270, in sympify > > raise ValueError("%r is NOT a valid SymPy expression" % a) > > ValueError: '<__main__.Foo instance at 0xb7dbf14c>' is NOT a valid > > SymPy expression > > > > So maybe we could check in the mul, if a ValueError exception gets > > raised in sympify and reraise NotImplemented instead? > > sympify(obj) should try obj.__sympy__() if obj is unknown to sympy. > If that fails then sympify should raise TypeError. > > For __mul__ method the sympify should take an optional argument, > say `return_on_unsuccess=NotImplemented`, and return NotImplemented.
Right. > Note that returning NotImplemented is not raising > NotImplementedError. You mean like return NotImplemented ? Wow, I didn't know about this: >>> def f(): ... return NotImplemented ... >>> f() NotImplemented >>> type(f()) <type 'NotImplementedType'> I learn something new everyday. :) Ondrej --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "sympy" group. To post to this group, send email to sympy@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sympy?hl=en -~----------~----~----~----~------~----~------~--~---