views:

106

answers:

2

I have a hash function in Python. It returns a value.

How do I see the byte-size of this return value? I want to know if it is 4-bytes or 8 or what.

Reason:

  • I want to make sure that the min value is 0 and the max value is 2**32, otherwise my calculations are incorrect.
  • I want to make sure that packing it to a I struct (unsigned int) is correct.

More specifically, I am calling murmur.string_hash(`x`). I want to know sanity-check that I am getting a 4-byte unsigned return value. If I have a value of a different size, them my calculations get messed up. So I want to sanity check it.

+1  A: 

If it's an arbitrary function that returns a number, there are only 4 standard types of numbers in Python: small integers (C long, at least 32 bits), long integers ("unlimited" precision), floats (C double), and complex numbers.

If you are referring to the builtin hash, it returns a standard integer (C long):

 >>> hash(2**31)
 -2147483648

If you want different hashes, check out hashlib.

Nick T
+1  A: 

Generally, thinking of a return value as a particular byte precision in Python is not the best way to go, especially with integers. For most intents and purposes, Python "short" integers are seamlessly integrated with "long" (unlimited) integers. Variables are promoted from the smaller to the larger type as necessary to hold the required value. Functions are not required to return any particular type (the same function could return different data types depending on the input, for example).

When a function is provided by a third-party package (as this one is), you can either just trust the documentation (which for Murmur indicates 4-byte ints as far as I can tell) or test the return value yourself before using it (whether by if, assert, or try, depending on your preference).

John Y