473,396 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

new-style classes, __hash__. is the documentation wrong?

hi,

if you have a new-style class,

it will have a __hash__ method, even if you don't define it,
because it inherits from object().

and, if you implement __eq__, then that default-hash-value will be
probably wrong.

i understand that this is a bug, and described here:

http://www.python.org/sf/660098

but please look at the documentation about __hash__ here:

http://docs.python.org/ref/customization.html

it says:

====
If a class defines mutable objects and implements a __cmp__() or
__eq__() method, it should not implement __hash__(), since the
dictionary implementation requires that a key's hash value is immutable
(if the object's hash value changes, it will be in the wrong hash bucket).
====

now, with new style classes, the class will have __hash__, whatever i
do. (well, i assume i could play with __getattribute__...).

so is that part of the documentation currently wrong?

because from what i see (the bug will not be fixed),
it's still much safer to define a __hash__ even for mutable objects
(when the object also defines __eq__).

gabor
Oct 25 '06 #1
1 1249
gabor wrote:
====
If a class defines mutable objects and implements a __cmp__() or
__eq__() method, it should not implement __hash__(), since the
dictionary implementation requires that a key's hash value is immutable
(if the object's hash value changes, it will be in the wrong hash bucket).
====
now, with new style classes, the class will have __hash__, whatever i
do. (well, i assume i could play with __getattribute__...).
There is a proposal to fix this, though I don't think we'll see it for
a while.

class CantHash(object):
__hash__ = None
so is that part of the documentation currently wrong?
yes
because from what i see (the bug will not be fixed),
it's still much safer to define a __hash__ even for mutable objects
(when the object also defines __eq__).
Better is to define __hash__ in a way that prevents errors:

class Mutable(object):
def __hash__(self):
raise TypeError('unhashable instance')

It will behave similarly to an old-style class that defines __eq__ and
not __hash__

-Mike

Oct 25 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Madestro | last post by:
Hi guys, I am making a small program to retrieve e-mails from POP accounts. I got all the e-mail parsing stuff figured out, but I cannot seem to come up with a way to find out which e-mails are...
3
by: Nimmi Srivastav | last post by:
There's a rather nondescript book called "Using Borland C++" by Lee and Mark Atkinson (Que Corporation) which presents an excellent discussion of overloaded new and delete operators. I am...
20
by: Razvan | last post by:
Hi ! Is there any difference between new X and new X() ?! Regards, Razvan
2
by: ip4ram | last post by:
I used to work with C and have a set of libraries which allocate multi-dimensional arrays(2 and 3) with single malloc call. data_type **myarray =...
11
by: Jonan | last post by:
Hello, For several reasons I want to replace the built-in memory management with some custom built. The mem management itlsef is not subject to my question - it's ok to the point that I have...
5
by: kUfa.scoopex | last post by:
Hi there! I have a small problem, and i really dont see any convenient way to fix it. Basically, i'd like to overload global new operators, into something like this: void *operator new(...
2
by: Dave | last post by:
Hello all, I'd like to find a source on the web that discusses, in a comprehensive manner and in one place, everything about new / delete. It should include overloading operator new, the new...
15
by: Steve | last post by:
I have a form with about 25 fields. In the BeforeUpdate event of the form, I have code that sets the default value of each field to its current value. For a new record, I can put the focus in any...
7
by: Mike Bulava | last post by:
I have created a base form that I plan to use throughout my application let call the form form1. I have Built the project then add another form that inherits from form1, I add a few panel controls...
3
by: Grizlyk | last post by:
Hi, people. Can anybody explain me "multiple 'new' at single line" behavior. Consider: p::p(void*); p::p(void*,void*); new A( p(new B), p( new C(p(new D), p(new E)) ), p(new F));
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.