if statement, with function inside it: if (t = Test()) == True:

Antoon Pardon apardon at forel.vub.ac.be
Mon May 4 10:25:44 EDT 2009


On 2009-04-24, Steven D'Aprano <steve at REMOVE-THIS-cybersource.com.au> wrote:
> On Fri, 24 Apr 2009 03:00:26 -0700, GC-Martijn wrote:
>
>> Hello,
>> 
>> I'm trying to do a if statement with a function inside it. I want to use
>> that variable inside that if loop , without defining it.
>> 
>> def Test():
>>     return 'Vla'
>> 
>> I searching something like this:
>> 
>> if (t = Test()) == 'Vla':
>>     print t # Vla
>> 
>> or
>> 
>> if (t = Test()):
>>     print t # Vla
>
> Fortunately, there is no way of doing that with Python. This is one 
> source of hard-to-debug bugs that Python doesn't have.

I think this is an unfortunate consequence of choosing '=' for the
assignment. They could have chosen an other token to indicate an
assignment one that would have made the difference between an
assignment and a comparison more visually different and thus
bugs by using one while needing the other less hard to track
down.

So when a certain kind of bug is hard to track down because of
the similarity of the tokens and not because of the specific
functionality I find it unfortunate they dropped the functionality
instead of making the tokens less similar.

-- 
Antoon Pardon



More information about the Python-list mailing list