Is there a Python equivalent to Ruby symbols?

PythonRubyDictionarySymbolsLanguage Comparisons

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

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.

Solution 2 - Python

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

Solution 3 - Python

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.

Solution 4 - Python

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

Solution 5 - Python

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpyloniconView Question on Stackoverflow
Solution 1 - Pythonsepp2kView Answer on Stackoverflow
Solution 2 - PythonNed BatchelderView Answer on Stackoverflow
Solution 3 - PythonMartianEView Answer on Stackoverflow
Solution 4 - PythonPhilippView Answer on Stackoverflow
Solution 5 - PythonMichael WolfView Answer on Stackoverflow