3.3. Special method names 29 The Python Language Reference, Release 3.9.5 User-defined classes have$_{__eq__()}$ and$_{__hash__()}$ methods by default; with them, all objects com- pare unequal (except with themselves) and x.__hash__() returns an appropriate value such that x == y implies both that$_{x is y}$ and$_{hash(x) == hash(y)}$ . A class that overrides __eq__() and does not define __hash__() will have its __hash__() implicitly set to$_{None}$ . When the$_{__hash__()}$ method of a class is$_{None}$ , instances of the class will raise an appropriate TypeError when a program attempts to retrieve their hash value, and will also be correctly identified as unhashable when checking$_{isinstance(obj, collections.abc.Hashable)}$ . If a class that overrides$_{__eq__()}$ needs to retain the implementation of$_{__hash__()}$ from a parent class, the interpreter must be told this explicitly by setting __hash__ = <ParentClass>.__hash__ . If a class that does not override$_{__eq__()}$ wishes to suppress hash support, it should include$_{__hash__}$ $_{= None}$in the class definition. A class which defines its own __hash__() that explicitly raises a TypeError would be incorrectly identified as hashable by an isinstance(obj, collections. abc.Hashable) call. Note: By default, the$_{__hash__()}$ values of str and bytes objects are “salted” with an unpredictable random value. Although they remain constant within an individual Python process, they are not predictable between repeated invocations of Python. This is intended to provide protection against a denial-of-service caused by carefully-chosen inputs that ex- ploit the worst case performance of a dict insertion, O(n^2) complexity. See http://www.ocert.org/advisories/ ocert-2011-003.html for details. Changing hash values affects the iteration order of sets. Python has never made guarantees about this ordering (and it typically varies between 32-bit and 64-bit builds). See also$_{PYTHONHASHSEED}$ . Changed in version 3.3: Hash randomization is enabled by default. The following methods can be defined to customize the meaning of attribute access (use of, assignment to, or deletion of$_{x.name}$ ) for class instances. Note that if the attribute is found through the normal mechanism,$_{__getattr__()}$ is not called. (This is an intentional asymmetry between$_{__getattr__()}$ and$_{__setattr__()}$ .) This is done both for efficiency reasons and because otherwise$_{__getattr__()}$ would have no way to access other attributes of the instance. Note that at least for instance variables, you can fake total control by not inserting any values in the instance attribute dictionary (but instead inserting them in another object). See the$_{__getattribute__()}$ method below for a way to actually get total control over attribute access. 3.3.2 Customizing attribute access object. __bool__ ( self$_{)}$ object. __getattr__ ( self , name$_{)}$ object. __getattribute__ ( self , name$_{)}$ Called when the default attribute access fails with an$_{AttributeError}$ (either$_{__getattribute__()}$ raises an$_{AttributeError}$ because name is not an instance attribute or an attribute in the class tree for self ; or$_{__get__()}$ of a name property raises$_{AttributeError}$). This method should either return the (computed) attribute value or raise an$_{AttributeError}$ exception. Called unconditionally to implement attribute accesses for instances of the class. If the class also defines __getattr__() , the latter will not be called unless$_{__getattribute__()}$ either calls it explicitly or raises an $_{AttributeError}$. This method should return the (computed) attribute value or raise an $_{AttributeError}$exception. In order to avoid infinite recursion in this method, its implementation should Called to implement truth value testing and the built-in operation$_{bool()}$ ; should return$_{False}$ or$_{True}$. When this method is not defined,$_{__len__()}$ is called, if it is defined, and the object is considered true if its result is nonzero. If a class defines neither$_{__len__()}$ nor$_{__bool__()}$ , all its instances are considered true.