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

Josiah Carlson josiah.carlson at gmail.com
Thu Sep 11 19:41:27 CEST 2008

On Thu, Sep 11, 2008 at 2:05 AM, Cliff Wells <cliff at develix.com> wrote:
> On Wed, 2008-09-10 at 22:47 -0700, Josiah Carlson wrote:
>> On Wed, Sep 10, 2008 at 9:34 PM, Cliff Wells <cliff at develix.com> wrote:
>> > On Wed, 2008-09-10 at 18:30 -0700, Josiah Carlson wrote:
>> >> On Wed, Sep 10, 2008 at 6:14 PM, Cliff Wells <cliff at develix.com> wrote:
>> As a data point; everyone that I've taught Python over the years
>> (undergrads and interns who only ever used Java before to 30-year
>> programming veterans of apl/cobol/fortran), not a single one of them
>> has ever had a problem with the difference between statements and
>> expressions.  It is possible that I've only ever worked with gifted
>> students and coworkers, though this is unlikely.
> Without any supporting data, my hunch is that most people who migrate to
> Python come from other imperative languages (myself included) and so
> don't find the distinction foreign.

Your hunch is likely correct.  I personally came from Scheme and
C/C++.  Many of Python's functional constructs fit the part of me that
got scheme, and everything else fit the part of me that got C/C++.

>> What I've found (historically) is that any time I find myself trying
>> to do something that is not available within the syntax of Python,
>> it's usually because it's a bad idea.  In your case, you want (for
>> example) the following to be valid Python...
>> lfcn = lambda x: (
>>     if x:
>>         True
>>     else:
>>         False)
>> Similarly...
>> lfcn = lambda x: (
>>     for i in xrange(x):
>>         yield fcn(i, x, ofcn(i))
>>     )
>> Why?  Initially it is for a templating language that you have designed
>> that makes these kinds of things difficult to do during an automatic
>> translation from working Python source code into compiled source code.
>>  Presumably something of the form...
>> html [ body [ a (href='http://python.org') ["Python!"] ] ]
>> To be converted into something like...
>> def generate_html(out):
>>     out.write('''<html>
>>   <body>
>>     <a href="http://python.org">Python!</a>
>>   </body>
>> </html>''')
> Actually, it's much simpler than that.   The HTML tags are objects which
> simply get serialized into their text representation.  I specifically do
> not generate Python source.  In fact, you can simply do something like
> this in the interactive interpreter:
>>>> print html [ body [ a (href='http://python.org') ["Python!"] ] ]
> <html><body><a href="http://python.org">Python!</a></body></html>
> This is more or less how Nevow Stan does it as well.

Not translating to Python source code is a lot less fun ;), and also
makes your templates far less dynamic in real-world scenarios.

> Overall, I'm left with the nagging suspicion that because of my language
> choice, I'm missing an important paradigm that would elevate my
> programming to the next level.

Then what the hell are you waiting around here for?  If you are
feeling constrained by Python (as a general-purpose language), try
some others!  You may want to add Haskell, Caml, or even Lua to your
list of languages to check out.  Maybe your Python will get better, or
maybe you'll move on from Python.  Who knows?

> In any case, I expect I'll stew in my current situation for a while
> longer at least.  The unfortunate fact remains that most of the
> languages that speak to me (e.g. Boo and Io) don't have the broad range
> of libraries and frameworks available that Python does and ultimately
> this tends to outweigh my esoteric desires.

One thing that you need to remember about Python is that it's a
general language.  It's syntax and semantics are such that it works in
a fairly large variety of situations.  Because it's a general
language, it sometimes doesn't do the things that would be convenient
in a domain specific language.  In particular, it doesn't have a lot
of those things that would make functional programming more convenient
(a shorter way of spelling 'lambda', everything is an expression,
etc.), but no one language can be perfect for everyone.

I wish you luck in your adventures with alternate languages.

 - Josiah

More information about the Python-ideas mailing list