[Python-ideas] Statements vs Expressions... why?

Josiah Carlson josiah.carlson at gmail.com
Thu Sep 11 01:24:31 CEST 2008

On Wed, Sep 10, 2008 at 2:57 PM, Adam Olsen <rhamph at gmail.com> wrote:
> On Wed, Sep 10, 2008 at 3:18 PM, Cliff Wells <cliff at develix.com> wrote:
>> Further, I feel that this limitation forces programmers into using hacks
>> and magic or overly spread-out code, which itself leads to readability
>> concerns.  Having used Python for around a decade, I'm quite aware of
>> the fact that you can happily write tons and tons of nice code with
>> Python in its current state.  However, because of the direction of my
>> work (developing a internal DSL in Python) I've suddenly become aware of
>> this glass ceiling.  I'd bumped into it before back when I was doing a
>> lot of GUI development, but blamed it on lambda rather than realizing
>> that it wasn't lambda so much as what I am bringing up now.
> Python is not intended for DSLs.  Really, don't do it.  Python is for
> python code.  If you want another language, write your own parser.  I
> hear lisp is simple to parse, and has no annoying statements to hold
> you back!
> Seriously though, there is an advantage to basing so much on
> statements rather than expressions.  We're specialized for one
> statement per line, which is the most common case, and it allowed us
> to have extraneous semicolons, braces, or whatever.  Readability
> benefits, style consistency benefits.
> Now there are some use cases that suffer here, including the one you
> just gave: defining a dispatch dict with the functions inline.  The
> best I could do is define the dict first, then stick a decorator on
> each function to register them.  That's still ugly though.  A creative
> solution is needed, but none come to mind.

class dispatcher(dispatch_dictionary):
    def x(value):
    def y(value):
    def int_23(value):
With the proper dispatch_dictionary base class and it's metaclass,
dispatcher would become a dictionary with functions that map from
strings and integers to function handlers.

Never underestimate the power of classes and metaclasses ;) .

 - Josiah

More information about the Python-ideas mailing list