4 ms·
I can't speak for GP, but I like to use single quotes for symbolic strings ('go_left', 'parent', 'alpha-campus', some_map['fixed_key']) and double quotes for te
by SpaceManiac 10y ago
I can't speak for GP, but I like to use single quotes for symbolic strings ('go_left', 'parent', 'alpha-campus', some_map['fixed_key']) and double quotes for textual strings ("An error occurred", "package/__main__.py").
- omaranto 10y agoOh, I like that idea: compensate via a convention for Python's lack of a symbol data type. :)
- jholman 10y agoIf you mean Ruby's symbols, Python has a data type with that set of properties. It's called 'str'. If you want the behaviour of Ruby strings, you might use a `bytes` or `bytearray` type, or you might use a list of chars.
- omaranto 10y agoI thought Ruby ensured there was only one symbol with given contents, so that equality testing for symbols reduces to pointer equality. I don't Python strings behave that way. But at any rate I wasn't thinking of performance characteristics but rather the stereotypical uses of symbols vs strings in languages that have both, so you are right that one can use Python strings in place of symbols, as SpaceManiac already pointed out above.
- BerislavLopac 10y ago> I thought Ruby ensured there was only one symbol with given contents, so that equality testing for symbols reduces to pointer equality. I don't Python strings behave that way. In Python, string literals are reused, if that is what you meant. >>> a = 'abc' >>> b = 'abc' >>> a is b True
- infinite8s 10y agoString literals up to a certain size are reused.
- omaranto 10y agoOh, cool, I didn't know that. I think in Ruby all symbols are reused, whether they are literals or constructed programatically. (Unlike Common Lisp symbols which can also de 'uninterned' if they are not literals.)
- gsnedders 10y agoThis isn't guaranteed by the language, even if CPython does it. What is guaranteed is intern('abc') is intern('abc').
- nerdponx 10y agoYup I do the same. some_dict = {'key': "value"}
- BerislavLopac 10y agoSometimes this convention falls apart, e.g. when constructing HTML tags it is easier (and more readable) to write: return '<a href="{}">{}</a>'.format(url, text) than return "<a href=\"{}\">{}</a>".format(url, text) Especially for a lazy developer like me... :)