Is there a Python equivalent to Ruby symbols?

pylonicon picture pylonicon · Sep 18, 2010 · Viewed 16.1k times · Source

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?

Answer

sepp2k picture sepp2k · Sep 18, 2010

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.