overloading hash() ? 
Author Message
 overloading hash() ?

Heya,
    How can i overload hash() to take a custom class and return a custom
computed hash value?

i note that hash(object) works for string and likely other built in types...
print hash ("Hello")

i would like to use this form of the function for say

temp = MyClass ()
print hash (temp)

thx



Thu, 22 May 2003 12:09:38 GMT  
 overloading hash() ?


Quote:
> Heya,
>     How can i overload hash() to take a custom class and return a custom
> computed hash value?

> i note that hash(object) works for string and likely other built in
types...
> print hash ("Hello")

> i would like to use this form of the function for say

> temp = MyClass ()
> print hash (temp)

Define a hash method for your class. For example:

class Hasher:
    def __hash__(self):
        return 58

h = Hasher()
print hash(h)

prints 58.



Thu, 22 May 2003 13:08:22 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Overloaded operator interprets block as hash

2. Hash.new(Hash.new) doesn't use Hash.new as default value

3. obj.hash now == obj.hash after?

4. Hash compression (Hash 'consing') circa 1957

5. hash as key in hash

6. Sorting a Hash by value of integer stored in the Hash

7. Hash#index !==> Hash#indexes

8. Hash#index !==> Hash#indexes

9. Hash.new {block} / Hash#default_proc{,_set}

10. Moron hashing (rehashing hashing again)

11. Hash compression (Hash 'consing') circa 1957

12. hash of hashes via ObjTypes?

 

 
Powered by phpBB® Forum Software