if statement, with function inside it: if (t = Test()) == True:
Rhodri James
rhodri at wildebst.demon.co.uk
Mon May 4 17:54:08 EDT 2009
On Mon, 04 May 2009 15:25:44 +0100, Antoon Pardon
<apardon at forel.vub.ac.be> wrote:
> 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.
What token could be used and still be meaningful, though? Algol
used ":=", which has most of the same problems as "=" (more, in my
opinion, since it fools the eye more easily if you're scanning
printed code quickly). Easily constructed arrows like "<=" or
"<-" collide with different comparators. About all that's left
that even vaguely implies assignment is "~", and it's no better.
--
Rhodri James *-* Wildebeeste Herder to the Masses
More information about the Python-list
mailing list