views:

145

answers:

3

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?

+3  A: 
  1. No, there is no equivalent.
  2. No you can use every hashable object as dictionary key.
Philipp
Can one assign a string literal to a variable and then use that variable as a key. e.g. orange = 'orange" food_type = {orange : 'fruit', banana : 'fruit'}
J3M 7OR3
@J3M 7OR3: Strings are hashable! As are all immutable types. Concerning the second question... Python is not Javascript - keys in dict literals are not special. They can be any expression (a variable, a literal, ...), are evaluated and the result is used as key. So yes, this works.
delnan
@delnan Ok. But is using a variable, instead of a string literal, as a key best practice? Do people typically do this? Or are they fine with using just string literals ( with their quotes) as keys?
J3M 7OR3
@J3M 7OR3: Wat? Most of the time, you can't possibly know the key by the time you write the program. Of course `k = "key"; d[k] = "value"` is stupid (but not `for k in keys: d[k] = "default"`, although there are better ways), but as already said, most of the time you use runtime-values as keys.
delnan
+10  A: 

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.

sepp2k
+1. Fortunately for us Ruby developers, performant is not a word. (Sorry, we're just so semantic!) :D
wuputah
@sepp2k Interesting. Thank you.
J3M 7OR3
Thie major benefit of this is that the language has one less core syntax and builtin type. It's unnecessary complexity in the language when it can be done transparently.
Glenn Maynard
@Glenn At first I was upset to hear that Python did not have symbols and that string literals can be used as keys. The problem is that I am not happy with the fact that I will have to place quotes around all my keys. Is there a way around this?
J3M 7OR3
@J3M 7OR3: No, of course there's no way around this. But consider: It's just one additional character, and in real code, you often use varibles instead of literal keys anyway.
delnan
@delnan, of course there is a way around this: dict(a=1,b=2,c=3).
Ned Batchelder
+3  A: 

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",
    )
Ned Batchelder
ironically, this solution _does_, at least initially, restrict you to having strings as the keys.
aaronasterling
yeah, it is a work around but only during the assignment step. i still have to add quotes around the b in d['b'] to get its value. i guess i just remember ruby symbols being easier to type. i wish there was a module that solved this problem. i wonder if it would make the program run slower though.
J3M 7OR3