Some syntactic sugar proposals
steve at REMOVE-THIS-cybersource.com.au
Tue Nov 16 11:42:39 CET 2010
On Mon, 15 Nov 2010 22:40:00 -0700, Ian Kelly wrote:
> On 11/15/2010 10:26 PM, Steven D'Aprano wrote:
>> t = foo()+bar()+baz() if pred(it) else baz()-foo()-bar()
>> What does "it" mean here?
> "it" would mean the result of the expression foo()+bar()+baz(). What
> else could it mean?
It could mean the last expression, baz(). Or the entire compound
> There are valid objections to the proposal, but the
> intended semantics seem perfectly clear.
Fair point, my example was terrible and didn't show the point that was
clear in my head. Mea culpa. How about this instead?
t = foo()+it if pred(it) else bar()
Should that be a SyntaxError, or is `it` a variable that holds its value
from statement to statement?
t = it
t = (foo() if pred(it) else bar()) if cond(it) else baz()
For what it's worth, Apple's defunct Hypertalk language had a couple of
syntax elements very much like that. Hypertalk had a special variable,
"it", which you used like this:
get the number of cards
put it into field "Card number"
I trust I don't have to explain this? :)
Hypertalk also had a special function, "the result", which worked
something like this:
ask "This is a dialog box. Please type your answer here:"
put the result into field "Your answer"
(or you could use "result()" instead). Both of these worked quite well
with Hypertalk, particularly with it's focus on non-programmers, but they
were designed into the language from the beginning. In Python they would
be the proverbial round peg in a square hole.
BTW, I frequently use "it" for iterators, and making "it" a reserved word
would break a lot of my code. This would make me quite peeved.
More information about the Python-list