[Edu-sig] the legality of naked expressions

Walter A. Aprile w.aprile@interaction-ivrea.it
Mon, 14 Oct 2002 09:24:23 +0200


Hello,
	I have noticed (or rather: my students have painfully noticed) that in
Python
it is legal but confusing to have certain expressions as statements. One
that seems
to bite people regularly in my class is the
function-reference-that-is-not-a-function-call:

def bemoan():
	print "Woe is me!"

a=1

if (a==1):
	bemoan
else:
	print "I am happy!"

	When run this code, obviously and correctly, produces absolutely
nothing --- but
puzzlement. Demonstrating that, at the prompt,

>>> bemoan
<function bemoan at 0x008B3C40>

	helps somewhat. Is there perhaps some warning that can be turned on in the
interpreter?
In the same vein, certain students find it perplexing that

if (a==1):
	1+1
else:
	2+2

	is perfectly legal yet looks a bit pointless - even if I realize that
__add__ could be
performing untold magic behind the scenes.

	I hope that I am not rehashing an old point here, but I haven't found this
in the PEPs. Thank you for your attention,

		Walter