[Python-ideas] Keyword/Symbol literals

Todd toddrjen at gmail.com
Wed Jan 21 08:58:51 CET 2015


On Jan 21, 2015 6:21 AM, "Matthew Rocklin" <mrocklin at gmail.com> wrote:
>
> 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.

At best this saves one character, may reduce readability, and would require
a huge amount of software to support another code path.  Is there any
advantage to this besides the one character saved?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150121/b71a8f26/attachment.html>


More information about the Python-ideas mailing list