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

G

GC-Martijn

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
 
S

Steven D'Aprano

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?
 
C

Chris Rebert

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
 
G

GC-Martijn

Fortunately, there is no way of doing that with Python. This is one
source of hard-to-debug bugs that Python doesn't have.


What's wrong with that?

Nothing is wrong with it , but it cost more lines (= more scrolling)
When possible I want to keep my code small.
 
G

GC-Martijn

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

Oke, thanks.
I will use the (correct) long way.
 
P

Paul Rubin

Chris Rebert said:
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.

One very common situation is matching something against a series of
regexps, and "ugly hacks" often end up being more attractive than "the
long way".

If you want to do it "the pointy headed way", start with the article

http://www.valuedlessons.com/2008/01/monads-in-python-with-nice-syntax.html
 
M

MRAB

GC-Martijn said:
Oke, thanks.
I will use the (correct) long way.
It's possible in C, but sometimes you might write '=' where you intended
'=='. It happens. I've done it myself. :-(

Sometimes it's an annoying restriction, but it does reduce bugs.
 
M

Marco Mariani

Ulrich said:
It unnecessarily injects the name 't' into the scope.

Since there is no concept in Python of a scope local to block
statements, I don't understant what you would like to happen instead.
 
L

Lawrence D'Oliveiro

In message <e7ee5c16-267c-4fef-
Nothing is wrong with it , but it cost more lines (= more scrolling)
When possible I want to keep my code small.

Maybe you'd be better off with Perl than Python.
 
A

Antoon Pardon

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.
 
T

Terry Reedy

There is a way to do something close.

def x(v):
x.val = v
return v

if x(3)==3:
print('x.val is ', x.val)
# prints
x.val is 3

In OP's case, condition is "x(Test()) == 'Vla'"
I think this is an unfortunate consequence of choosing '=' for the
assignment.

Actually, is is a consequence of making assignment a statement rather
than an expression. And that is because if assignment were an
expression, the new name would have to be quoted. Or there would have
to be a special exception to the normal expression evaulation rule. In
other words, the problems one sees in a pure expression language. Note
that C, for instance, which has such a special exception, does not, last
I knew, allow a,b = 1,2.

The solution for this use case is to encapsulate the binding within a
function. The above is one possible example. One could use a pocket
class instead of a pocket function. Or

def set_echo(name, val):
globals()[name] = val
return val

if set_echo('t', Test()) == 'Vla': print t ...

Terry Jan Reedy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top