[Python-ideas] Combining test and assignment

Chris Rebert pyideas at rebertia.com
Sun Jan 22 22:59:47 CET 2012


> On Sun, Jan 22, 2012 at 1:36 PM, Chris Rebert <pyideas at rebertia.com> wrote:
>> On Sun, Jan 22, 2012 at 4:04 AM, Paul Moore <p.f.moore at gmail.com> wrote:
>> > On 22 January 2012 04:51, Steven D'Aprano <steve at pearwood.info> wrote:
>> >> Assignment as an expression feels unnatural to me:
>> >>
>> >> if spam() as x != 'ham':
>> >>    frobulate(x)
>> >>
>> >> doesn't really correspond to any natural English order.
>> >
>> > While in general I agree with you, there *is* a natural reading of this:
>> >
>> > "if spam() isn't 'ham', frobulate it"
>> >
>> > The fact that you have to choose a name is because computer languages
>> > have to be more explicit than natural languages, and can't deal with
>> > the implicit referent involved in the English usage of "it". The need
>> > for assignment comes from the need for a name. You could actually deal
>> > with the if-or-while-expression-assignment case by defining a special
>> > variable __it__ to mean the value of the condition in the containing
>> > if or while expression, but it has all the same lack of generality
>> > issues as any proposal that limits itself to if/while conditions. And
>> > short of explicitly marking the expression somehow, you can't
>> > generalise any further.
>>
>> This is reminiscent of Perl's $_ variable, which represents the "current
>> topic".
>> http://www.perl.com/pub/2002/10/30/topic.html

On Sun, Jan 22, 2012 at 1:24 PM, Edward Lesmes <ehlesmes at gmail.com> wrote:
> why not make python do something like
> _ = test()
> if _:
>     ...
> Automatically, so you can do something like
>
> if test():
>     frobulate(_)
>
>
> It seems natural to me, because in prompt,
> python assigns the last result to the
> _ variable

I think "Explicit is better than implicit"[0] pretty much covers why
that sort of thing is generally disfavored.

Cheers,
Chris
--
[0]: http://www.python.org/dev/peps/pep-0020/



More information about the Python-ideas mailing list