[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: expressions vs. statements



Paul Graham wrote:
> 
> This is actually a question people on this list might
> have interesting opinions about.  Does distinguishing
> between expressions and statements buy you anything in
> expressive power?  I have often wondered about what
> the point of this distinction was...

One thing you to be careful of with infix languages is not allowing
assignment to look almost the same as equality testing in an expression
context. Contrary to what someone said on the list today, Python does
not have this problem.

Also, it is debatable what the "right thing" for an expression like this
to return is:

k = while foo():
       print "abc"
       print "def"
       print "ghi"

I guess it would return None (Python's nil/null thing)? Allowing people
to assign the results of a while loop just seems obfuscatory and thus
un-pythonic.

In Perl, you often see code like this:

print "abc";
print "def";
5;  # returns 5

That's just weird to me...the value of the last statement in a block is
just the value of the last statement in a block. If you want it to be
the value of the block you should say so. And if we're going to have
while loops that return values that are not useful (like None) then why
even allow you to pretend that the while loop is an expression?

Finally, Python tries to encourage people to code in a similar style at
the micro-level and express their creativity at the macro level. This
makes other people's Python code easier to read. So you will always see
this:

if a:
   j=5
else:
   j=6

and never:

j = if a: 5 else: 6

 Paul Prescod