Is there a Python equivalent to Ruby symbols?

后端 未结 5 1086
时光说笑
时光说笑 2020-12-13 23:39

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

相关标签:
5条回答
  • 2020-12-14 00:00

    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",
        )
    
    0 讨论(0)
  • 2020-12-14 00:04
    1. No, there is no equivalent.
    2. No you can use every hashable object as dictionary key.
    0 讨论(0)
  • 2020-12-14 00:10

    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.

    0 讨论(0)
  • 2020-12-14 00:13

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

    0 讨论(0)
  • 2020-12-14 00:21

    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.

    0 讨论(0)
提交回复
热议问题