[Python-ideas] Keyword/Symbol literals

Masklinn masklinn at masklinn.net
Wed Jan 21 11:05:04 CET 2015

On 2015-01-21, at 10:13 , Andrew Barnert <abarnert at yahoo.com.dmarc.invalid> wrote:
> On Jan 20, 2015, at 23:48, Stephen Hansen <me+python at ixokai.io> wrote:
>> What do people do now?
>> or strings
>> df.sort('name')
>> ...
>> strings work but feel unpleasant
>> Why?
>> What does :foo get you that 'foo' doesn't?
>> One character being saved doesn't seem like a good reason to add syntax.
> Mainly they're used to fill in gaps where a language isn't dynamic enough to just use strings, which Python doesn't need.

That's debatable, various languages have used them for fairly different
reasons. AFAIK:

* Smalltalk had symbols because its strings are mutable, not internable
  and O(n) comparison (implementation may vary, Squeak's literal strings
  are/were immutable but I do not know if they were interned). Symbols are
  always interned and provide O(1) comparison which is useful in many
* Ruby inherited behaviour and purpose (the community did later add
   syntactic sugar on top of symbols, like `&:foo` for {|o| o.foo})
* Erlang uses symbols (which it calls atoms) because its strings are
   linked lists of integers and thus relatively expensive; atoms are
   literal and interned with identity (across the process too), historically
   it expected such a small number of atoms they weren't even GC'd
* Lisp's use of symbols comes from the metaprogramming side: how do
  you represent a symbol (a variable name) in a macro? With a string it's
  not possible to differentiate actual strings and strings-as-identifiers.
  So symbols it is. And since symbols were in, cheap and useful they 
  could be exposed to the developer.

It's got nothing to do with "dynamic enough", that's just nonsense, it
has to do with the semantics of the language and its strings, and the
string's suitability to some specific purposes or use cases.

Which does not mean symbols should be added to Python (let alone
that all languages should have symbols).
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150121/896606ab/attachment-0001.html>

More information about the Python-ideas mailing list