Hash of class from instance

J

Joakim Storck

Hello,

Is there any way that I can find the hash value of a class from an
instance?
.... pass
....
What I want is a function that returns the value of 'hash(A)':
a.getHashOfClass()
10782976

Is this possible?

/Joakim
 
D

Duncan Booth

Joakim said:
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__)
 
J

Joakim Storck

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
 
M

Mick Krippendorf

Joakim said:
[...] 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:

Mick.
 
S

Steven Bethard

Mick said:
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
 
J

Joakim Storck

So I guess it might be a little bit less unwise to use id() instead
then...

/Joakim
 
J

Just

"Joakim Storck said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,219
Messages
2,571,117
Members
47,729
Latest member
taulaju99

Latest Threads

Top