[Python-ideas] Keyword/Symbol literals

Matthew Rocklin mrocklin at gmail.com
Wed Jan 21 06:20:43 CET 2015


It would be nice to have literal keywords/symbols.  By this I mean, terms
that look like words but are not previously defined variables.

*Prior art*

Some other languages prefix by colon, :foo,  or use a backtick,  `foo.

julia> :foo
:foo
julia> typeof(:foo)
Symbol

user=> :foo
:foo
user=> (type :foo)
clojure.lang.Keyword

user=> `foo
user/foo
user=> (type `foo)
clojure.lang.Symbol

*Why is this useful?*

One use case in NumPy/Pandas use is to specify columns or fields of data
without resorting to strings.  E.g.

df = pandas.load(...)
df.sort(:name)

*What do people do now?*

Currently people use auto-generated attributes

df.sort(df.name)

or strings

df.sort('name')

auto-generated attributes work great until you want to use chained
expressions

df.change_dataframe().sort(some_new_column_not_in_df)

strings work but feel unpleasant

*Prior discussion?*

This is a common language construct so my guess is that it has come up
before.  Sadly Google searching the terms *keywords* and *symbols* results
it a lot of unrelated material.  Can anyone point me to previous discussion?

There are clearly issues with using :foo in that it overlaps with slice
syntax, presumably some other character could be pressed into service if
this was found worthwhile.

I can come up with more motivating use cases if desired.

Best,
-Matthew
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150120/4130e6da/attachment-0001.html>


More information about the Python-ideas mailing list