Constraints on __sub__, __eq__, etc.

Jean-Michel Pichavant jeanmichel at sequans.com
Thu Feb 18 11:58:54 EST 2010


Andrey Fedorov wrote:
>
>     It may be intuitive to you, but its not true, written down
>     anywhere, nor assumed by the language, and the mathematical
>     meaning of the operators doesn't matter to Python. Python
>     purposefully does not enforce anything for these methods.
>
>
> Right, so neither is anything in PEP-8, but it's still considered 
> "good practice". I'm running across examples like you gave (__sub__ 
> having a side-effect on the left-hand operand) in some code, and am 
> trying to find concrete justification for avoiding it.
>
> - Andrey

This is all about communication. Python allows you to call a cat a dog. 
You'll confuse everyone if you do so, but you can do it.
If your '-' operator is doing something else than returning the sub 
result, it's your call, but do not expect people to get it easily. You'd 
better be good at writting the documentation :-)

JM




More information about the Python-list mailing list