Skip to main content

Home/ Mac Attack/ Group items tagged comparison

Rss Feed Group items tagged

Benjamin Bandt-Horn

jcalderone: How to override comparison operators in Python - 0 views

  • here are the basic rules for the customization of ==, !=, <, >, <=, and >=: For all six of the above operators, if __cmp__ is defined on the left-hand argument, it is called with the right-hand argument. A result of -1 indicates the LHS is less than the RHS. A result of 0 indicates they are equal. A result of 1 indicates the LHS is greater than the RHS
  • __eq__ is not used for !=
  • For <, __lt__ is used. For >, __gt__. For <= and >=, __le__ and __ge__ respectively
  • ...1 more annotation...
  • __eq__ does an isinstance test on its argument
  •  
    here are the basic rules for the customization of ==, !=, , =: For all six of the above operators, if __cmp__ is defined on the left-hand argument, it is called with the right-hand argument. A result of -1 indicates the LHS is less than the RHS. A result of 0 indicates they are equal. A result of 1 indicates the LHS is greater than the RHS.
Benjamin Bandt-Horn

3. Data model - Python v2.6.6 documentation - 0 views

  • it is advised to somehow mix together (e.g. using exclusive or) the hash values for the components of the object that also play a part in comparison of objects.
  • If a class does not define a __cmp__() or __eq__() method it should not define a __hash__() operation either
  • Called when an attribute lookup has not found the attribute in the usual places (i.e. it is not an instance attribute nor is it found in the class tree for self). name is the attribute name. This method should return the (computed) attribute value or raise an AttributeError exception.
  • ...2 more annotations...
  • new-style classes
  • By default, instances of both old and new-style classes have a dictionary for attribute storage
  •  
    special method names
Benjamin Bandt-Horn

Elegant ways to support equivalence ("equality") in Python classes - Stack Overflow - 0 views

  • mixin class
  • Another issue with the __dict__ comparison is what if you have an attribute that you don't want to consider in your definition of equality (say for example a unique object id, or metadata like a time created stamp).
  • isinstance sucks
  • ...4 more annotations...
  • if type(other) is type(self):
  • Check types more strictly, like this:
  • is tests for object identity. This means a is b will be True in the case when a and b both hold the reference to the same object
  • __cmp__ was removed from python 3 so avoid it
  •  
    mixin class
1 - 3 of 3
Showing 20 items per page