assignment in if

G

Gary Wessle

Hi

is there a way to make an assignment in the condition of "if" and use
it later, e.g.

nx = re.compile('regex')
if nx.search(text):
funCall(text, nx.search(text))

nx.search(text) is evaluated twice, I was hoping for something like

nx = re.compile('regex')
if x = nx.search(text):
funCall(text, x))

thanks
 
B

Ben Caradoc-Davies

Gary said:
is there a way to make an assignment in the condition of "if"
No.

nx = re.compile('regex')
if x = nx.search(text):
funCall(text, x))

Use:

nx = re.compile('regex')
x = nx.search(text)
if x:
funCall(text, x)
 
R

Roy Smith

Gary Wessle said:
Hi

is there a way to make an assignment in the condition of "if" and use
it later, e.g.

nx = re.compile('regex')
if nx.search(text):
funCall(text, nx.search(text))

nx.search(text) is evaluated twice, I was hoping for something like

nx = re.compile('regex')
if x = nx.search(text):
funCall(text, x))

Personally, I find the C-style idiom you long for to be convenient and
useful. That being said, it does not exist in Python, by deliberate design
decision. In Python, assignment is not an operator with side effects like
in C or Java, but a statement. What you need to do is:

nx = re.compile('regex')
x = nx.search(text)
if x:
funCall(text, x))

The lack of embedded assignments leads to slightly more verbose code in
situations like this, but on the other hand, it avoids the typical C
disaster of writing a whole function as a one liner.
 
E

Edward Elliott

Roy said:
decision. In Python, assignment is not an operator with side effects like
in C or Java, but a statement.

<nitpick> assignemnt is actually an expression in those languages, not a
statement. said:
The lack of embedded assignments leads to slightly more verbose code in
situations like this, but on the other hand, it avoids the typical C
disaster of writing a whole function as a one liner.

Writing disasters in python just takes a little more creativity. ;)
 
E

Edward Elliott

Edward said:
s/statement/operator/

<shakes head>
it was probably clearer the first time, but let me rephrase:
in C/Java, assignment is neither a statement not an operator. it is an
expression involving a certain operator. that operator (not so)
coincidentally happens to be the assignment operator, or one of its
variants.

ah forget it, replying to your reply to yourself is probably illegal...
 
B

Bruno Desthuilliers

Gary Wessle a écrit :
Hi

is there a way to make an assignment in the condition of "if" and use
it later
No.

, e.g.

nx = re.compile('regex')
if nx.search(text):
funCall(text, nx.search(text))

nx.search(text) is evaluated twice, I was hoping for something like

nx = re.compile('regex')
if x = nx.search(text):
funCall(text, x))

x = nx.search(text)
if x:
funCall(text, x)
 

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
474,294
Messages
2,571,511
Members
48,202
Latest member
ClaudioVil

Latest Threads

Top