[Edu-sig] the legality of naked expressions

Tim Peters tim.one@comcast.net
Mon, 14 Oct 2002 21:13:46 -0400


[Walter A. Aprile]
> ...
> 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.

If you type that into an interactive Python session, and a is defined,
you'll find that it actually displays one of 2 or 4.  That's a "deeper
reason" here.  The same thing that's confusing your students is also
delighting them when they do stuff like

>>> 1+2
3
>>>

interactively.  In fact, in its *very* early days, Python printed the value
of "raw expression statements" in interactive mode (which it still does)
*and* in batch mode (which it no longer does).

>>> for i in range(10):
...     i*i
...
0
1
4
9
16
25
36
49
64
81
>>>