Also how to do one-liners? ;)
Also how to do one-liners? ;)
http://python-history.blogspot.com/2009/02/early-language-de...
1. "Open" statements (Pascal):
IF x
THEN s1
ELSE s2;
IF x
THEN
BEGIN
s1;
s2
END
ELSE
BEGIN
s3;
s4
END
WHILE c DO s;
WHILE c DO
BEGIN
s1;
s2
END
The problem with this design was need to use blocks (BEGIN ... END) and complex rules in case of nesting.2. "Closed" statements (Algol 68)
IF x
THEN
s1;
s2
ELSE
s3;
s4
FI // END IF
WHILE c DO
s1;
s2
OD // END WHILE
You can see new kind of parenthesis IF-FI, DO-OD, etc.I more like "closed" syntax. But C syntax with it's brackets blocks "{...}" instead of "BEGIN...END" made this less an issue.
if a == 42: print "Yes" ; do_stuff()
It's true that you cannot have a "else" statement on the same line, however.But in some cases, you could use a conditional expression instead [2]:
x = f() if a == 42 else g()
[1] http://docs.python.org/reference/compound_stmts.html[2] http://docs.python.org/reference/expressions.html#conditiona...
x = (a==42) ? f() : g();
f() if a==42 else g()
looks much more readable to me than f() if a == 42 else g()Which is more readable:
> condition ? value1 : value2
or > value1 if condition else value2
Maybe the latter is just more readable to me because I'm used to reading Perl all day long that looks like: > die unless condition;
> return true if condition;
> return false
> if condition1 && condition2 && condition3;
That said, I'm also used to reading: > return condition1 ? value1
> : condition2 ? value2
> : condition3 ? value3
> : value4;
and I still find the Python ternary operator more readable: > return value1 if condition1 else
> value2 if condition2 else
> value3 if condition3 else
> value4 x = (a==42) and f() or g()So I would prefer they were not possible :)
See my last attempt in Feb 2009: http://mail.python.org/pipermail/python-ideas/2009-February/...
lambda x: x*2
http://diveintopython.org/power_of_introspection/lambda_func...
Programmers can learn syntax. Regular humans can't. Both Python and Ruby failed to provide a simple way of building internal DSLs. In Python humans need to be specific about indentation and ":" and in Ruby they need to remember all these stupid sigils.