Constraints on __sub__, __eq__, etc.

Benjamin Kaplan benjamin.kaplan at case.edu
Thu Feb 18 11:37:52 EST 2010


On Thu, Feb 18, 2010 at 11:19 AM, Andrey Fedorov <anfedorov at gmail.com> wrote:
> It seems intuitive to me that the magic methods for overriding the +, -, <,
> ==, >, etc. operators should have no sideffects on their operands. Also,
> that == should be commutative and transitive, that > and < should be
> transitive, and anti-commutative.
> Is this intuition written up in a PEP, or assumed to follow from the
> mathematical meanings?
> - Andrey

There are no assumptions about anything. You can do whatever you want
when you implement the functions yourself. For the numeric types, they
follow the intuitive meanings but that's not necessary for user
defined types. Of course, your users will get very confused if a == b
but b != a. Or if a == b and a != b at the same time.


> --
> http://mail.python.org/mailman/listinfo/python-list
>
>



More information about the Python-list mailing list