if statement, with function inside it: if (t = Test()) == True:
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 -- The long way t = Test() if (t == 'Vla': print t # must contain Vla Greetings, GCMartijn -- http://mail.python.org/mailman/listinfo/python-list
Re: if statement, with function inside it: if (t = Test()) == True:
On 24 apr, 12:11, Steven D'Aprano 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. > > > -- The long way > > t = Test() > > if (t == 'Vla': > > print t # must contain Vla > > What's wrong with that? > > -- > Steven- Tekst uit oorspronkelijk bericht niet weergeven - > > - Tekst uit oorspronkelijk bericht weergeven - Nothing is wrong with it , but it cost more lines (= more scrolling) When possible I want to keep my code small. -- http://mail.python.org/mailman/listinfo/python-list
Re: if statement, with function inside it: if (t = Test()) == True:
On 24 apr, 12:15, Chris Rebert wrote: > On Fri, Apr 24, 2009 at 3:00 AM, 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 > > > -- > > The long way > > t = Test() > > if (t == 'Vla': > > print t # must contain Vla > > Disregarding some ugly hacks, Python does not permit assignment in > expressions, so what you're asking for is not possible. > For the goods of readability, prevention of errors, and simplicity, > Python forces you to do it the "long way" (the Right Way(tm) if you > ask most Python partisans). > > If you could explain your situation and the context of your question > in greater detail, someone might be able to suggest an alternate > structure for your code which obviates your desire for such a > construct. > > Cheers, > Chris > -- > I have a blog:http://blog.rebertia.com- Tekst uit oorspronkelijk bericht niet > weergeven - > > - Tekst uit oorspronkelijk bericht weergeven - Oke, thanks. I will use the (correct) long way. -- http://mail.python.org/mailman/listinfo/python-list