Planning a Python Course for Beginners

Steven D'Aprano steve+comp.lang.python at pearwood.info
Thu Aug 10 04:04:43 EDT 2017


On Wed, 09 Aug 2017 20:07:48 +0300, Marko Rauhamaa wrote:

> Good point! A very good __hash__() implementation is:
> 
>     def __hash__(self):
>         return id(self)
> 
> In fact, I didn't know Python (kinda) did this by default already. I
> can't find that information in the definition of object.__hash__():


Hmmm... using id() as the hash would be a terrible hash function. Objects 
would fall into similar buckets if they were created at similar times, 
regardless of their value, rather than being well distributed. But let's 
see whether or not objects actually do so, as you claim:

>>> a, b, c, d = "abc", "def", "ghi", "jki"
>>> [id(obj) for obj in (a,b,c,d)]
[139932454814752, 139932454814808, 139932454814920, 139932454913616]
>>> [hash(obj) for obj in (a,b,c,d)]
[7231609897320296628, -876470178105133015, -5049894847448874792, 
5697571649565117128]



Wait, maybe you're referring to hash() of object(), inherited by classes 
that don't define their own __hash__. Let's check it out:

>>> a, b, c, d = [object() for i in range(4)]
>>> [id(obj) for obj in (a,b,c,d)]
[139932455747696, 139932455747712, 139932455747728, 139932455747744]
>>> [hash(obj) for obj in (a,b,c,d)]
[8745778484231, 8745778484232, 8745778484233, 8745778484234]



Maybe object does something different for itself than for subclasses?

>>> class X(object):
...     pass
... 
>>> a, b, c, d = [X() for i in range(4)]
>>> [id(obj) for obj in (a,b,c,d)]
[139932454939952, 139932454939896, 139932454940008, 139932454940064]
>>> [hash(obj) for obj in (a,b,c,d)]
[8745778433747, -9223363291076342065, -9223363291076342058, 8745778433754]



I see zero evidence that Python uses id() as the default hash.

Not even for classic classes in Python 2.




-- 
“You are deluded if you think software engineers who can't write 
operating systems or applications without security holes, can write 
virtualization layers without security holes.” —Theo de Raadt



More information about the Python-list mailing list