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