Author steven.daprano
Recipients Dima.Tisnek, steven.daprano
Date 2018-12-28.03:35:19
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1545968119.99.0.931531567008.issue35600@roundup.psfhosted.org>
In-reply-to
Content
> In the old days I'd just `hash(some_variable)` but of course now I cannot.

I'm sorry, I don't understand... why can't you?

py> text = "NOBODY expects the Spanish Inquisition!"
py> hash(text)
1245575277


There's also this:

py> hashlib.md5(text.encode('utf-8')).digest()
b'@\xfb[&\t]\x9c\xc0\xc5\xfcvH\xe8:\x1b]'


although it might be a bit expensive if you don't care about security and too weak if you do. Can you explain why hash() isn't suitable?

For what's its worth, I wouldn't use sum() to generate a hash since it may be unbounded and may not be "mixed up" enough. If you can't hash a string, perhaps you can hash a tuple of ints?

py> hash(tuple(map(ord, text)))
-816773268
py> hash(tuple(map(ord, text+"\0")))
667761418
History
Date User Action Args
2018-12-28 03:35:21steven.dapranosetrecipients: + steven.daprano, Dima.Tisnek
2018-12-28 03:35:19steven.dapranosetmessageid: <1545968119.99.0.931531567008.issue35600@roundup.psfhosted.org>
2018-12-28 03:35:19steven.dapranolinkissue35600 messages
2018-12-28 03:35:19steven.dapranocreate