Is there a Python equivalent to Ruby symbols?

拜拜、爱过 提交于 2019-11-28 22:13:29
sepp2k

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.

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",
    )

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.

  1. No, there is no equivalent.
  2. No you can use every hashable object as dictionary key.

Not as a first-class type but there does exist https://pypi.python.org/pypi/SymbolType.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!