In Python, this is a valid program
def PrintWorld():
print('World')
print('Hello')
0
'beautiful'
True
None
0 < 0
3, 3
lambda c:3
PrintWorld()
All it does is print 'Hello World' on two lines. The expressions are 0
, 'beautiful'
, True
, None
, 0 < 0
, 3, 3
, and lambda c:3
are ignored and I'm wondering why.
I realize that this behavior allows Python to treat the function call PrintWorld()
like any expression: It is an expression with value None
in the course of whose evaluation the function is called. In a sense, this means that both 'beautiful'
, PrintWorld()
, and a hypothetical expression with side effects like PrintWorld() is None
could be treated the same on some level.
I also realize that this is related to interactive mode: When you run the Python interactively, giving only a stand-alone expression will result in its value being printed.
Still, why should non-interactive mode accept these expressions (except function calls)? In my eyes they open up the possibility for pointless code without offering anything in return.
And, as a follow-up: If you do allow these stand-alone expressions, why have comments or a pass
statement in the language when you could just use a stand-alone expression instead? Sure, "explicit is better than implicit", but it's also valuable to keep the language small.
As research I googled and searched stack overflow and CS stack exchange. I also looked into the Python language definition.