473,396 Members | 2,090 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.

Hashability of classes, old and new

Hi, people. I observe that there is a difference in hashability between
old-style and new-style instances, when they are comparable. I wonder
what I meaning I should make out of this. Maybe the simplest is to first
show an example:

---------------------------------------------------------------------->
Python 2.3.3 (#1, Jan 24 2004, 09:01:30)
[GCC 3.3 20030226 (prerelease) (SuSE Linux)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
class C: .... __metaclass__ = type
.... def __cmp__(a, b):
.... print 'C', a, b
.... return 0
.... class D: .... import types
.... __metaclass__ = types.ClassType
.... def __cmp__(a, b):
.... print 'D', a, b
.... return 0
.... c = C()
d = D()
hash(C) 136172876 hash(D) 1076805372 hash(c) 1076846924 hash(d) Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: unhashable instance
----------------------------------------------------------------------<

As you see, `C' is new-style, `D' is old-style. (I could have written
declarations more simply, if __metaclass__ was not preset to type within
my `.pythonrc' -- in any case, the above writing is very explicit. :-)

Is there a change in semantics in this area? For new-style, I have:

----------------------------------------------------------------------> c1 = C()
c2 = C()
z = {}
z[c1] = 101
z[c2] = 102
c1 == c2 C <__main__.C object at 0x402f650c> <__main__.C object at 0x402f64ec>
True z[c1] 101 z[c2]

102
----------------------------------------------------------------------<

So, despite c1 equals c2, they create two different entries in a
dictionary. I thought that this is such an inconsistency that was the
reason behind requiring an explicit `__hash__' for old-style classes.
So, it seems I was not thinking right, there is something which escapes
my understanding. Would some kind soul help mine? :-)

--
François Pinard http://www.iro.umontreal.ca/~pinard

Jul 18 '05 #1
1 1137
> So, despite c1 equals c2, they create two different entries in a
dictionary. I thought that this is such an inconsistency that was the
reason behind requiring an explicit `__hash__' for old-style classes.
So, it seems I was not thinking right, there is something which escapes
my understanding. Would some kind soul help mine? :-)


When two objects have different hashes, they are not required to be
compared when inserted into a dictionary. They may be compared, if two
hashes drop the objects into the same bin.

- Josiah
Jul 18 '05 #2

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

Similar topics

5
by: Chris S. | last post by:
Perhaps this is obvious to some, but why are dictionary keys constrained to hashable objects? why not use the object's id for the hash value. Wouldn't this allow typically non-hashable objects to...
1
by: Bob Rock | last post by:
Hello, in the last few days I've made my first few attempts at creating mixed C++ managed-unmanaged assemblies and looking aftwerwards with ILDASM at what is visible in those assemblies from a...
9
by: Jack | last post by:
Hello I have a library of calculationally intensive classes that is used both by a GUI based authoring application and by a simpler non-interactive rendering application. Both of these...
9
by: Aguilar, James | last post by:
I know that one can define an essentially unlimited number of classes in a file. And one can declare just as many in a header file. However, the question I have is, should I? Suppose that, to...
12
by: Langy | last post by:
Hello I'm fairly new to C++ but have programmed several other languages and found most of c++ fairly easy (so far!). I've come to a tutorial on classes, could someone please tell me why you...
2
by: joye | last post by:
Hello, My question is how to use C# to call the existing libraries containing unmanaged C++ classes directly, but not use C# or managed C++ wrappers unmanaged C++ classes? Does anyone know how...
18
by: Edward Diener | last post by:
Is the packing alignment of __nogc classes stored as part of the assembly ? I think it must as the compiler, when referencing the assembly, could not know how the original data is packed otherwise....
6
by: ivan.leben | last post by:
I want to write a Mesh class using half-edges. This class uses three other classes: Vertex, HalfEdge and Face. These classes should be linked properly in the process of building up the mesh by...
0
by: ivan.leben | last post by:
I am writing this in a new thread to alert that I found a solution to the problem mentioned here: http://groups.google.com/group/comp.lang.c++/browse_thread/thread/7970afaa089fd5b8 and to avoid...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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.