Attack a sacred Python Cow

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Thu Jul 24 05:26:40 EDT 2008


Jordan a écrit :

(snip rant about self and __eq__ / __ne__)

1/ about the __eq__ / __ne__ stuff:

Please get your facts, the behaviour *is* actually fully documented:

"""
There are no implied relationships among the comparison operators. The 
truth of x==y does not imply that x!=y is false. Accordingly, when 
defining __eq__(), one should also define __ne__() so that the operators 
will behave as expected.
"""
http://docs.python.org/ref/customization.html

FWIW, the __lt__ / __le__ / __eq__ / __ne__ / __gt__ / __ge__ methods 
set, known as "rich comparisons", was added in Python 2.1 to give more 
fine-grained control on comparisons. If you don't need such a 
granularity, just implement the __cmp__ method and you'll have all 
comparison operators working as expected.

2/ wrt/ self in functions signatures:

How would you handle this case with an implicit 'self' :

class Foo(object):
    pass

def bar(self):
    print self

Foo.bar = bar






More information about the Python-list mailing list