4

When python builtin hash() is just wired cross-platform. I have an app use builtin hash() for 'test'. Both systems are 64bit, python 2.7.12

windows:

>>> hash('test')
1308370872

linux:

>>> hash('test')
2314058222102390712

Why is this?

thefourtheye
  • 233,700
  • 52
  • 457
  • 497
wiwengweng
  • 352
  • 1
  • 4
  • 16

1 Answers1

4

There are no guarantees about the value hash returns in Python. It appears that you're using a 32-bit Windows Python (that's a guess), and that you're using a 64-bit python on linux (again, a guess). IIRC (and I haven't checked), The default hash(item) returns the address of item as its hash value.

If you want to have values you can compare across operating systems, look at hashlib.

cco
  • 5,873
  • 1
  • 16
  • 21
  • I just reread the question; `hash(item)` returning the address of `item` could return wildly different values betweens different runs on one platform, much less on different ones, regardless of 32/64-bit issues. – cco Oct 13 '16 at 07:19
  • Worse, two strings with equal value don't necessarily have the same memory address, but they need to have the same hash or they could appear twice in dictionary keys. So this can't be it. – RemcoGerlich Oct 13 '16 at 07:23
  • Yep. Python does us a lot of favors by interning strings transparently, so `hash('foo')` could be either the same or different from `xx='foo'; hash(xx)`. `hashlib` has solutions for this problem. – cco Oct 13 '16 at 07:33
  • hashlib is completely immune to the "problem" as it simply doesn't care about the address, the hash depends on the string's value. Same for hash(). – RemcoGerlich Oct 13 '16 at 07:46
  • 1
    True, but hash() isn't guaranteed to be platform-independent, and hashlib is. – cco Oct 13 '16 at 07:50