Also for those interested: the characters in Ruby when used in a hash are very similar to empty objects in python. For example, you can do:
some_var = object()
and then set the dictionary key as some_var:
some_dict = { some_var : 'some value' }
and then do a standard search:
some_dict[some_var]
However, since sepp2k noted that there is no performance benefit. In fact, I did a quick test and added little to the performance increase:
a, b, c, d, e = [object() for _ in range(5)] dict_symbols = {a : 'a', b : 'b', c : 'c', d : 'd', e : 'e'} dict_strings = {'a' : 'a', 'b' : 'b', 'c' : 'c', 'd' : 'd', 'e' : 'e'} def run_symbols(): for key in dict_symbols.keys(): dict_symbols[key] def run_strings(): for key in dict_strings.keys(): dict_strings[key]
Speed ββtested in ipython:
In [3]: %timeit run_symbols 10000000 loops, best of 3: 33.2 ns per loop In [4]: %timeit run_strings 10000000 loops, best of 3: 28.3 ns per loop
So, in my case, βcharactersβ are slower! (for funny numbers, inaccurate). However, it should be noted that there are probably memory advantages for this. Unless you care that key type objects are smaller than strings.
import sys sys.getsizeof('some_var')
Although this raises the question of how python handles the memory of the variable name some_var.