[Python-ideas] Statements vs Expressions... why?
Cliff Wells
cliff at develix.com
Thu Sep 11 21:45:06 CEST 2008
On Thu, 2008-09-11 at 14:56 -0400, Mike Meyer wrote:
> Here's where we disagree. I love functional programming, having
> written 10s of thousands of lines of LISP in the past. The lack of the
> ability to pass around functions in Ruby is why I haven't ever written
> any.
You can:
def a(x,y)
x + y
end
def x(b)
send(b,3,4)
end
puts x(:a) # 7
The semantics are a bit different, but the result is the same (caveat:
I'm no Ruby expert by any stretch of the imagination).
Anyway, not important =)
> And I consider the results of trying to cram python statements
> into expressions as almost universally ugly. I'm willing to consider
> you might have a solution, but you refuse to *show* it to me.
I'm not sure what else you'd like to see. I've provided an example with
a for-loop as generator and if-expressions. I've no intention of
writing a full PEP covering all use-cases at this point.
> > Overall, if you are familiar with functional programming, it doesn't
> > take a lot to imagine more examples. If you aren't, then it probably
> > won't make much sense or seem too appealing in any case.
>
> You're right - it's easy to image in more examples. Really, really
> ugly ones, like:
>
> a = (
> if x < 23:
> 18
> else:
> 24
> ) + (
> if y < 23:
> 29
> else:
> 81
> )
>
> The thought of running into something like the above in production
> code is enough to make me bounce pretty much any proposal that would
> allow it. Compared to that,
>
> a = (18 if x < 23 else 24) + (29 if y < 23 else 81)
a = ( if x < 23: 18 else: 24 ) + ( if y < 23: 29 else: 81 )
Granted, today this is not valid Python, but supporting it doesn't seem
to break existing Python code either. This is precisely how Logix does
it (from the page I linked you to earlier):
x = if 2+2==4: "I thought so" else: "hmmm..."
In any case, your above code seems intentionally formatted to be
unreadable, something that could be achieved under any circumstances.
For example, here's your code reformatted in a fashion similar to your
first example:
a = (
18
if x < 23
else 24
) + (
29
if y < 23
else 81
)
Just as unreadable (although really I don't find it terrible to
comprehend... certainly no worse than a complicated listcomp).
At the end of the day, no programming language is going to be able to
enforce sane coding style (even Python's whitespace can be circumvented
to a large degree).
What we do gain with the idea I've forwarded is a single, more flexible
if-expression in place of an if-statement and an if-operator with
inverted syntax.
Regards,
Cliff
More information about the Python-ideas
mailing list