Stay up to date – embedded 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 486 486 487 487 488 488 Being Contained By Another Object 489 489 --------------------------------- 490 490 491 Enforcing Uniqueness 492 ******************** 491 493 | ``__hash__(self)`` 492 494 | ``hash(x)`` <==> ``x.__hash__()`` 493 495 | Called by the built-in function ``hash``, and to compare **keys** in dictionary operations. 494 496 | 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?). 496 498 | Hash value should be immutable (always?). 497 |498 499 499 500 500 501 Descriptors 501 502 *********** 502 503 **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.