Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 18th, 2005, 09:59 PM
Joakim Storck
Guest
 
Posts: n/a
Default Hash of class from instance

Hello,

Is there any way that I can find the hash value of a class from an
instance?
[color=blue][color=green][color=darkred]
>>> class A:[/color][/color][/color]
.... pass
....[color=blue][color=green][color=darkred]
>>> a = A()
>>> hash(A)[/color][/color][/color]
10782976[color=blue][color=green][color=darkred]
>>> hash(a)[/color][/color][/color]
12251904[color=blue][color=green][color=darkred]
>>>[/color][/color][/color]

What I want is a function that returns the value of 'hash(A)':[color=blue]
> a.getHashOfClass()[/color]
10782976

Is this possible?

/Joakim

  #2  
Old July 18th, 2005, 09:59 PM
Duncan Booth
Guest
 
Posts: n/a
Default Re: Hash of class from instance

Joakim Storck wrote:
[color=blue]
> Is there any way that I can find the hash value of a class from an
> instance?
>[/color]

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

hash(a.__class__)

  #3  
Old July 18th, 2005, 09:59 PM
Joakim Storck
Guest
 
Posts: n/a
Default Re: Hash of class from instance

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

  #4  
Old July 18th, 2005, 10:00 PM
Mick Krippendorf
Guest
 
Posts: n/a
Default Re: Hash of class from instance

Joakim Storck wrote:[color=blue]
> [...] the hash values of classes will be used as
> keys in a dictionary that serve as an object pool. [...][/color]

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:
[color=blue][color=green][color=darkred]
>>> my_pseudo_hash_code = 1
>>> my_dict = {my_pseudo_hash_code:"gnarf",[/color][/color][/color]
my_pseudo_hash_code:"snarf"}[color=blue][color=green][color=darkred]
>>> print my_dict[/color][/color][/color]

Mick.

  #5  
Old July 18th, 2005, 10:00 PM
Steven Bethard
Guest
 
Posts: n/a
Default Re: Hash of class from instance

Mick Krippendorf wrote:[color=blue]
> In Python there seems to be no guarantee that different objects also
> have different hash values.[/color]

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
  #6  
Old July 18th, 2005, 10:00 PM
Joakim Storck
Guest
 
Posts: n/a
Default Re: Hash of class from instance

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

/Joakim

  #7  
Old July 18th, 2005, 10:00 PM
Just
Guest
 
Posts: n/a
Default Re: Hash of class from instance

In article <1107373593.789905.168850@o13g2000cwo.googlegroups .com>,
"Joakim Storck" <joakim.storck@home.se> wrote:
[color=blue]
> So I guess it might be a little bit less unwise to use id() instead
> then...[/color]

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

Just
  #8  
Old July 18th, 2005, 10:00 PM
Joakim Storck
Guest
 
Posts: n/a
Default Re: Hash of class from instance

Simply because it didn't occur to me. So now I have
[color=blue][color=green][color=darkred]
>>> class A: pass[/color][/color][/color]
....[color=blue][color=green][color=darkred]
>>> d = {A:[A(),A(),A()]}
>>> a = d[A].pop()
>>> a[/color][/color][/color]
<__main__.A instance at 0x00F09A58>[color=blue][color=green][color=darkred]
>>>[/color][/color][/color]

Thanks all!

/Joakim

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles