473,473 Members | 2,015 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Hash of class from instance

Hello,

Is there any way that I can find the hash value of a class from an
instance?
class A: .... pass
.... a = A()
hash(A) 10782976 hash(a) 12251904

What I want is a function that returns the value of 'hash(A)': a.getHashOfClass()

10782976

Is this possible?

/Joakim

Jul 18 '05 #1
7 1966
Joakim Storck wrote:
Is there any way that I can find the hash value of a class from an
instance?


You only had to post the question once. It seems a strange thing to want,
but just do:

hash(a.__class__)

Jul 18 '05 #2
Thanks!

Not so strange I think, the hash values of classes will be used as keys
in a dictionary that serve as an object pool.

Sorry about the double posting, I got a 'server error' message the
first time, so I figured it hadn't gone trhough.

/Joakim

Jul 18 '05 #3
Joakim Storck wrote:
[...] the hash values of classes will be used as
keys in a dictionary that serve as an object pool. [...]


That does seem unwise (as Teal'c would have uttered). The spec says:

----
hash( object)

Return the hash value of the object (if it has one). Hash values are
integers. They are used to quickly compare dictionary keys during a
dictionary lookup. Numeric values that compare equal have the same hash
value (even if they are of different types, as is the case for 1 and
1.0).
----

Normally the following should hold (not mentioned in the above spec but
true for Java, e.g.): if a and b are objects such that a equals b then
hash(a) equals hash(b). This does not imply that if hash(a) equals
hash(b) also a equals b. More formally: (a == b) -> (hash(a) ==
hash(b)).

In Python there seems to be no guarantee that different objects also
have different hash values. So let's assume we have class objects Foo
and Bar, which by some unlikely incident happen to have the same hash
values, then storing them in a dictonary under their respective hash
values (which are identical) would most probably lead into a problem,
secifically the problem that you'd end up accessing Foo when you indeed
think you are accessing Bar, or vice versa. Just try this:
my_pseudo_hash_code = 1
my_dict = {my_pseudo_hash_code:"gnarf", my_pseudo_hash_code:"snarf"} print my_dict


Mick.

Jul 18 '05 #4
Mick Krippendorf wrote:
In Python there seems to be no guarantee that different objects also
have different hash values.


Well, it's true that you can override the __hash__ method to do whatever
you want, but I believe the default for class __hash__ methods is to
return the class id, which should be different for each class:

py> class C(object):
.... pass
....
py> hash(C)
12699152
py> id(C)
12699152

Steve
Jul 18 '05 #5
So I guess it might be a little bit less unwise to use id() instead
then...

/Joakim

Jul 18 '05 #6
In article <11**********************@o13g2000cwo.googlegroups .com>,
"Joakim Storck" <jo***********@home.se> wrote:
So I guess it might be a little bit less unwise to use id() instead
then...


Why don't you use the class objects themselves as dict keys?

Just
Jul 18 '05 #7
Simply because it didn't occur to me. So now I have
class A: pass .... d = {A:[A(),A(),A()]}
a = d[A].pop()
a <__main__.A instance at 0x00F09A58>


Thanks all!

/Joakim

Jul 18 '05 #8

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

Similar topics

0
by: Joakim Storck | last post by:
Hello, Is there any way that I can find the hash value of a class from an instance? >>> class A: .... pass .... >>> a = A() >>> hash(A)
3
by: Markus Dehmann | last post by:
I have a class "Data" and I store Data pointers in an STL set. But I have millions of inserts and many more lookups, and my profiler found that they cost a lot of runtime. Therefore, I want to...
31
by: Alexis Nikichine | last post by:
Hello, It is common knowledge that arrays can be used as hashtables: var color = ; color = 0xFF0000; color = 0x0000FF;
47
by: VK | last post by:
Or why I just did myArray = "Computers" but myArray.length is showing 0. What a hey? There is a new trend to treat arrays and hashes as they were some variations of the same thing. But they...
2
by: Bryan Olson | last post by:
The current Python standard library provides two cryptographic hash functions: MD5 and SHA-1 . The authors of MD5 originally stated: It is conjectured that it is computationally infeasible to...
5
by: Opa | last post by:
Hi Guys I'm having trouble getting a value from a hash table based on a key that I have added The key is the handle to a socket and the value is an instance of a class called SckClientHandler...
8
by: mortb | last post by:
Hi, How do I write a GenerateHashcode function that will generate guaranteed unique hashcodes for my classes? cheers, mortb
0
by: krigare | last post by:
There maybe a better way of doing this but I am attempting to display a list of objects and their respective properties using a datagridview. The object class is straight forward with several...
8
by: Jim Cobban | last post by:
I am writing a program in which I need a hash table implementation of a Map. The version of g++ available for Windo$e does not yet include the TR1 support for this. It just has the original SGI...
23
by: raylopez99 | last post by:
A quick sanity check, and I think I am correct, but just to make sure: if you have a bunch of objects that are very much like one another you can uniquely track them simply by using an ArrayList...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.