Is the only issue with overloading __eq__ that hashing goes away (thus not allowing use of SymPy objects inside dicts/sets, etc...)? Apparently numpy gets around this by defining a __hash__ method. Could we do the same? Are there other concerns with overloading __eq__?
Consider the following example. >>> class foo: ... def __init__(self, a, b): ... self.a, self.b = a, b ... def __eq__(self, other): ... return other ... def __hash__(self): ... return hash((self.a, self.b)) >>> f = foo(1,2) >>> f == 5 5 >>> set((f, 5)) set([<__main__.foo instance at 0x9866bcc>, 5]) On May 24, 12:46 pm, Ronan Lamy <ronan.l...@gmail.com> wrote: > Le mardi 24 mai 2011 à 12:28 -0500, Andy Ray Terrel a écrit : > > > > > > > > > > > On Tue, May 24, 2011 at 11:42 AM, Robert Kern <robert.k...@gmail.com> wrote: > > > On Tue, May 24, 2011 at 10:44, Vinzent Steinberg > > > <vinzent.steinb...@googlemail.com> wrote: > > >> On 24 Mai, 05:08, Matthew <mrock...@gmail.com> wrote: > > >>> You're right - it's unclear if this should be an event or a random > > >>> variable. Thanks for the heads up on 'or'. I was hoping to use | for > > >>> 'given' in the future. I'll figure this out when I get there. Isn't > > >>> '==' ok to use though? Isn't it __eq__? I thought that 'is' was the > > >>> forbidden one. > > > >> '==' is forbidden too, because it is Python equality and not > > >> mathematical equality. E.g. in Python: > > > >>>>> (x + 1)**2 == x**2 + 2*x + 1 > > >> False > > > > Note that what Vinzent and Ronan mean is not that *Python* forbids > > > overriding __eq__ to return other objects. numpy, SQLAlchemy, and > > > several other libraries do that just fine. Rather, sympy has decided, > > > as a matter of *policy*, to define __eq__ methods in such a way as to > > > return a bool, like most other types, in order to allow sympy objects > > > to be used in a variety of contexts like most Python objects you will > > > encounter. In particular, most sympy objects can be used as dictionary > > > keys or set members. > > > You can use the relationals instead: > > > In [3]: sympy.Eq(x, 2*y) > > Out[3]: x == 2*y > > You will probably have to deal with issue 1887, though. > > http://code.google.com/p/sympy/issues/detail?id=1887 -- 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 sympy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sympy?hl=en.