[Python-ideas] Anaphoric if

Brett Cannon brett at python.org
Sat Apr 24 20:34:06 CEST 2010


On Sat, Apr 24, 2010 at 11:05, Andrey Fedorov <anfedorov at gmail.com> wrote:

> On Sat, Apr 24, 2010 at 1:13 PM, Ron Adam <rrr at ronadam.com> wrote:
>
>>  On 04/23/2010 07:23 PM, Jess Austin wrote:
>>
>> > for the sake of completeness: would the following:
>> >
>> > if not foo() as x:
>> >      ... x
>> >
>> > mean this:
>> >
>> > x = foo()
>> > if not x:
>> >      ... x
>>
>>
>> I would think it would defined as
>>
>>     if expression as name:
>>         block using name
>>
>> Which would be...
>>
>>    x = not foo()
>>    if x:
>>        ... x
>>
>> So it could be tricky to get the correct behavior unless (expression as
>> name) becomes a valid expression in it self, but then it wouldn't be limited
>> to the if block, so you might as well just use (expression = name).
>>
>
> Err, that is *not* "correct behavior". This is the third time I'm repeating
> myself (not including the subject): I'm talking about an *anaphoric if*.
> That is an if statement which can refer back to its predicate, nothing
> more.
>
>
Well, I suspect I am not alone with having never heard of the term
"anaphoric if" (and Google backs me up; this very thread is now search
result #4 for [anaphoric if]). It seems to come from the Lisp world and is
literally nothing more than syntactic sugar for inlining the assignment
statement of what is being tested by an 'if' statement. So no fancy
restrictive scoping of the variable; this would be dead-simple syntactic
sugar.

Unless I am wrong in which case poor Andrey will have to repeat himself a
fifth time. =)

But back to the ``if not foo() as x`` example, that does water down this
syntax for me. I would suspect that the places where this occurs I would
want what foo() returned to be assigned to x instead of ``not foo()`` as the
latter is guaranteed to be a boolean and not some false object that I might
want to populate with stuff to make true.

-Brett
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20100424/100aff61/attachment.html>


More information about the Python-ideas mailing list