473,320 Members | 2,083 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

assignment in if

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
May 2 '06 #1
7 1664
In article <87************@localhost.localdomain>,
Gary Wessle <ph****@yahoo.com> wrote:
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.
May 2 '06 #2
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)
May 2 '06 #3
Gary Wessle wrote:
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)

--
Ben Caradoc-Davies <be*@wintersun.org>
http://wintersun.org/
"Those who deny freedom to others deserve it not for themselves."
- Abraham Lincoln
May 2 '06 #4
Roy Smith wrote:
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. </nitpick>
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. ;)

May 3 '06 #5
Edward Elliott a écrit :
Roy Smith wrote:

(snip)
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. ;)

+1 QOTW
May 3 '06 #6
Edward Elliott wrote:
<nitpick> assignemnt is actually an expression in those languages, not a
statement. </nitpick>


s/statement/operator/
May 3 '06 #7
Edward Elliott wrote:
Edward Elliott wrote:
<nitpick> assignemnt is actually an expression in those languages, not a
statement. </nitpick>


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

May 3 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Paul Rubin | last post by:
OK, I want to scan a file for lines matching a certain regexp. I'd like to use an assignment expression, like for line in file: if (g := re.match(pat, line)): croggle(g.group(1)) Since...
10
by: Andrew Koenig | last post by:
It has been pointed out to me that various C++ books disagree about the relative precedence of ?: and the assignment operators. In order to satisfy myself about the matter once and for all, I...
5
by: CoolPint | last post by:
It seems to me that I cannot assign objects of a class which has a constant data member since the data member cannot be changed once the constructor calls are completed. Is this the way it is meant...
16
by: Edward Diener | last post by:
Is there a way to override the default processing of the assignment operator for one's own __value types ? I realize I can program my own Assign method, and provide that for end-users of my class,...
9
by: Rick N. Backer | last post by:
I have an abstract base class that has char* members. Is an assignment operator necessary for this abstract base class? Why or why not? Thanks in advance. Ken Wilson Amer. Dlx. Tele,...
1
by: Jon Slaughter | last post by:
I have a chain of classes(i.e., a series of classes each containing an array of the next class). Each class has array like access. struct Myclass1 { vector(Myclass2) _Myclass2; Myclass2&...
166
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
6
by: Neil Zanella | last post by:
Hello, I would like to know whether the following C fragment is legal in standard C and behaves as intended under conforming implementations... union foo { char c; double d; };
35
by: nagy | last post by:
I do the following. First create lists x,y,z. Then add an element to x using the augumented assignment operator. This causes all the other lists to be changed also. But if I use the assignment...
20
by: TimeHorse | last post by:
I would like to gauge interest in the following proposal: Problem: Assignment statements cannot be used as expressions. Performing a list of mutually exclusive checks that require data...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.