Python hash() function on strings - python

How does a hash value of some particular string is calculated in CPython2.7?
For instance, this code:
print hash('abcde' * 1000)
returns the same value even after I restart the Python process and try again (I did it many times).
So, it seems that id() (memory address) of the string doesn't used in this computation, right? Then how?

Hash values are not dependent on the memory location but the contents of the object itself. From the documentation:
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).
See CPython's implementation of str.__hash__ in:
Objects/unicodeobject.c (for unicode_hash)
Python/pyhash.c (for _Py_HashBytes)

Related

Accessing Unicode Values in a Python Dictionary

I have a dictionary full of unicode keys/values due to importing JSON through json.loads().
dictionaryName = {u'keyName' : u'valueName'}
I'm trying to access values inside the dictionary as follows:
accessValueName = dictionaryName.get('keyName')
This returns None, assumedly because it is looking for the String 'keyName' and the list is full of unicode values. I tried sticking a 'u' in front of my keyName when making the call, but it still returns none.
accessValueName = dictionaryName.get(u'keyName')
I also found several seemingly outdated methods to convert the entire dictionary to string values instead of unicode, however, they did not work, and I am not sure that I need the entire thing converted.
How can I either convert the entire dictionary from Unicode to String or just access the values using the keyname?
EDIT:
I just realized that I was trying to access a value from a nested dictionary that I did not notice was nested.
The solution is indeed:
accessValueName = dictionaryName.get('keyName')
Dictionaries store values in a hash table using the hash values of the object.
print(hash(u"example"))
print(hash("example"))
Yields the same result. Therefore the same dictionary value should be accessible with both.

python dictionaries are hashmaps of pointers to...what?

I know that in python, dictionaries are implemented by hashing the keys and storing pointers (to the key-value pairs) in an array, the index being determined by the hash.
But how are the key-value pairs themselves stored? Are they stored together (ie, in contiguous spots in memory)? Are they stored as a tuple or array of pointers, one pointing to the key and one to the value? Or is it something else entirely?
Googling has turned up lots of explanations about hashing and open addressing and the like, but nothing addressing this question.
Roughly speaking there is a function, let's call it F, which calculates an index, F(h), in an array of values. So the values are stored as an array and they are looked up as F(h). The reason it's "roughly speaking", is that hashes are computed differently for different objects. For example, for pointers, it's p>>3; while for strings, hashes are digests of all the bytes of a string.
If you want to look at the C code, search for lookdict_index or just look at the dictobject.c file in CPython's source code. It's pretty readable if you are used to reading C code.
Edit 1:
From the comment in Python 3.6.1's Include/dictobject.h:
/* If ma_values is NULL, the table is "combined": keys and values
are stored in ma_keys.
If ma_values is not NULL, the table is splitted:
keys are stored in ma_keys and values are stored in ma_values */
And an explanation from dictobject.:
/*
The DictObject can be in one of two forms.
Either:
A combined table:
ma_values == NULL, dk_refcnt == 1.
Values are stored in the me_value field of the PyDictKeysObject.
Or:
A split table:
ma_values != NULL, dk_refcnt >= 1
Values are stored in the ma_values array.
Only string (unicode) keys are allowed.
All dicts sharing same key must have same insertion order.
....
*/
The values are either stored as an array of strings which follows an array of "key objects". or each value's pointer is stored in the me_value of PyDictKeyEntry. The keys are stored in me_key fields of PyDictKeyEntry. The array of keys is really an array of PyDictKeyEntry structs.
Just as a reference, PyDictKeyEntry is defined as:
typedef struct {
/* Cached hash code of me_key. */
Py_hash_t me_hash;
PyObject *me_key;
PyObject *me_value; /*This field is only meaningful for combined tables*/
} PyDictKeyEntry;
Relevant files to look at: Objects/dict-common.h, Objects/dictobject.c, and Include/dictobject.h in Python's source code.
Objects/dictobject.c has an extensive write up in the comments in the beginning of the file explaining the whole scheme and historical background.

What really takes up memory in Python?

Suppose I have 1000 different objects of the same class instantiated, and I assign it to a dictionary whose keys are integers from 1 to 1000, and whose values are those 1000 objects.
Now, I create another dictionary, whose keys are tuples (obj1, 1), (obj2,2), etc. The obj's being those same 1000 objects. And whose values are 1 to 1000.
Does the existence of those 2 dictionaries mean that memory usage will be doubled, because 1000 objects are in each dict's key and values?
They should NOT be, right? Because we are not creating new objects, we are merely assigning references to those same objects. So I can have 1000 similar dictionaries with those objects as either values or as keys (part of a tuple), and have no significant increases in memory usage.
Is that right?
Objects are not copied, but referenced.
If your objects are small (e.g. integers), the overhead of a list of tuples or a dict is significant.
If your objects are large (e.g. very long unique strings), the overhead is much less, compared to the size of the objects, so the memory usage will not increase much due to creation of another dict / list of the same objects.

How do I use a python dictionary object in MATLAB?

I'm playing with the new capability in MATLAB 2014b where you can call python directly from matlab and get python objects in the workspace (like you've been able to with Java for a long time). I have successfully called a function and gotten a dictionary into the workspace, but I'm stuck on how to harvest the values from it. In this case I have a dictionary full of dictionaries, so I can't just convert it to MATLAB cells like they do in their example.
So a specific question: if I have a dictionary in MATLAB called "A", how to I get the sub-dictionary A['2'] out?
By consulting the MATLAB External Interfaces documentation, the Python dict type is essentially the same interface as a containers.Map. You need to use the values function to extract the value that you want for a certain key. As such, you would call values like so, given that your Python dictionary is stored in A and you want to use '2' as the key to index into your dictionary:
val = values(A, '2');
As such, val would contain what the associated value is with the key of '2'. MATLAB also has the ability to use multiple keys and it would return multiple values - one per key. Therefore, you could also do something like this:
val = cell(values(A, {'1', '2', '3'}));
val would be a three element cell array, where each element is the value for the associated keys that you input in. It is imperative that you convert the output of values into a cell array, because this would normally be a list in Python. In order to use these results in MATLAB, we need to convert to a cell.
Therefore, val{1} would be the dictionary output when you used '1' as the key. Similarly, val{2} would be the dictionary output when you used '2' as the key and so on.
Here are some more operations on a containers.Map object for you. If you want all of the keys in a dictionary, use the keys function:
k = keys(A);
If you were to just use values with the dictionary by itself, like so:
val = cell(values(A));
It would produce all values for every key that exists in your dictionary stored into a cell array.
If you wanted to update a particular key in your Python dictionary, use the update function:
update(A,py.dict(pyargs('4',5.677)));
Here, we use the dictionary A, and update the key-value pair, where the key is '4' and the new value is 5.677.

Why doesn't Python hash lists using ID?

When using a dictionary in Python, the following is impossible:
d = {}
d[[1,2,3]] = 4
since 'list' is an unhashable type. However, the id function in Python returns an integer for an object that is guaranteed to be unique for the object's lifetime.
Why doesn't Python use id to hash a dictionary? Are there drawbacks?
The reason is right here (Why must dictionary keys be immutable)
Some unacceptable solutions that have been proposed:
Hash lists by their address (object ID). This doesn’t work because if you construct a new list with the same value it won’t be found; e.g.:
mydict = {[1, 2]: '12'}
print mydict[[1, 2]]
would raise a KeyError exception because the id of the [1, 2] used in the second line differs from that in the first line. In other words, dictionary keys should be compared using ==, not using is.
It is a requirement that if a == b, then hash(a) == hash(b). Using the id can break this, because the ID will not change if you mutate the list. Then you might have two lists that have equal contents, but have different hashes.
Another way to look at it is, yes, you could do it, but it would mean that you could not retrieve the dict value with another list with the same contents. You could only retrieve it by using the exact same list object as the key.
In Python dictionaries keys are compared using ==, and the equality operator with lists does an item-by-item equality check so two different lists with the same elements compare equal and they must behave as the same key in a dictionary.
If you need to keep a dictionary or set of lists by identity instead of equality you can just wrap the list in a user-defined object or, depending on the context, may be you can use a dictionary where elements are stored/retrieve by using id explicitly.
Note however that keeping the id of an object stored doesn't imply the object will remain alive, that there is no way for going from id to object and that id may be reused over time for objects that have been garbage collected. A solution is to use
my_dict[id(x)] = [x, value]
instead of
my_dict[id(x)] = value

Categories