问题
Is there a Python equivalent to Ruby symbols?
If so then what is it?
If not then are we stuck with using strings as our keys in dictionaries only?
回答1:
No, python doesn't have a symbol type.
However string literals are interned by default and other strings can be interned using the intern function. So using string literals as keys in dictionaries is not less performant than using symbols in ruby.
回答2:
As others have said, there is no symbol in Python, but strings work well.
To avoid quoting strings as keys, use the dict() constructor syntax:
d = dict(
a = 1,
b = 2,
c = "Hello there",
)
回答3:
Also for those interested: symbols in Ruby when used in a hash are very similar to empty objects in python. For example you could do:
some_var = object()
and then set a dictionary key as some_var:
some_dict = { some_var : 'some value' }
and then do a standard retrieval:
some_dict[some_var]
However, as sepp2k noted there is no performance benefit in doing this. In fact I did a quick test and noted little to no performance boost:
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 the 'symbols' run slower! (for fun numbers, not accurate). However it is to note that there are probably memory advantages to doing it this way. If you don't care about the key type objects have a smaller footprint than strings.
import sys
sys.getsizeof('some_var') # 45
some_var = object()
sys.getsizeof(some_var) # 0
Although that raises the question of how python treats the memory of the variable name some_var.
回答4:
- No, there is no equivalent.
- No you can use every hashable object as dictionary key.
回答5:
Not as a first-class type but there does exist https://pypi.python.org/pypi/SymbolType.
来源:https://stackoverflow.com/questions/3743532/is-there-a-python-equivalent-to-ruby-symbols