Hide
Stay up to dateembedded code automagically updates, each snippet and article has a feed Join Siafoo Now or Learn More

Python __Underscore__ Methods

Revision 5 vs. Revision 6

Changelog: Updates to __hash__

Legend:

Unmodified
Added
Removed
  • Content

    r5 r6  
    486486  
    487487  
    488488Being Contained By Another Object  
    489489---------------------------------  
    490490  
     491Enforcing Uniqueness  
     492********************  
    491493| ``__hash__(self)``  
    492494|   ``hash(x)`` <==> ``x.__hash__()``  
    493495|   Called by the built-in function ``hash``, and to compare **keys** in dictionary operations.  
    494496|   Should return a 32-bit integer.  Objects which evaluate as equal should have the same hash value.  Starting in Python 2.5, may instead return a long integer; that object's __hash__ will be used.  
    495 |   Only should be defined if ``__cmp__`` is defined; if ``__cmp__`` or ``__eq__`` are defined and ``__hash__`` is not, object cannot be used as a dictionary key.  If none of the three are defined, object **can** be used as a dictionary key (why?).  
     497|   Only should be defined if ``__cmp__`` is defined; if ``__cmp__`` or ``__eq__`` are defined and ``__hash__`` is not, object **cannot be used as a dictionary key or in a set**.  If none of the three are defined, object **can** be used as a dictionary key or in a set (why?).  
    496498|   Hash value should be immutable (always?).  
    497 |  
    498499  
    499500  
    500501Descriptors  
    501502***********  
    502503**New-style classes** may possess attributes called "descriptors", which are themselves **new-style** class instances.  With the following functions, descriptors can change what will happen when the descriptor is accessed, set, or deleted.