[Python-ideas] Accepting keyword arguments for __getitem__

Paul Moore p.f.moore at gmail.com
Mon Jun 23 19:32:58 CEST 2014


On 23 June 2014 18:11, Terry Reedy <tjreedy at udel.edu> wrote:
> As others have pointed out, you are not actually asking that __getitem__
> 'accept keyword arguments'.  Rather you are asking that "x=y" be seen as an
> abbreviation for "{'x':y}" in a very rare usage in a particular context to
> save 4 (admittedly awkward) keystrokes.

The point here is that the OP is viewing Python syntax as (in effect)
a DSL[1] for his application, and is looking for syntactical support
for constructs that make sense in the context of that DSL. It's not
about saving keystrokes, but about expressing things in a way that
matches the problem space. The problem is that Python doesn't really
support use as a DSL (as opposed to, say Ruby and Perl, which have
syntax that is explicitly designed for use as a DSL).

Trying to add on DSL-style syntax into Python is always going to be
difficult, because that's not how the language was designed. On the
other hand, writing a parser or preprocessor that handles a specific
DSL is entirely possible - just painful because you need to handle all
the niggly details of expression parsers, etc.

Maybe a better approach would be to add features to the Python parser
to allow it to be used in 3rd party code and customised. Applications
could then more easily write their own Python-derived syntax, with a
parser that can read from a string, or even implement an import hook
to allow directly importable DSL files. I don't know how practical
this solution is, or how much of it is already available, but it might
be a more productive way of directing people who are looking for
"python-like" syntax for their application languages, rather than
simply leaving them with writing their own parser, or trying to get
Python's syntax changed (which is essentially not going to happen).

Just a thought...

Paul

[1] Domain Specific Language, just in case the term isn't familiar.


More information about the Python-ideas mailing list