test assignmet problem

P

Paolo Pantaleo

So I tried this

if(not (attr=global_re.match(line)) ):
break

it says invalid syntax [on the =]
so it is not possible to do test and assignment in C style?
how can I write this otherwise?

Thnx
PAolo
 
D

Duncan Booth

Paolo said:
So I tried this

if(not (attr=global_re.match(line)) ):
break

it says invalid syntax [on the =]
so it is not possible to do test and assignment in C style?
how can I write this otherwise?
With fewer parentheses for a start, but all you have to do here is to do
the assignment and the test on separate lines:

attr = global_re.match(line)
if not attr:
break

In other cases you may have to work a bit harder to restructure your code.
 
P

Paul McGuire

So I tried this

if(not (attr=global_re.match(line)) ):
break

it says invalid syntax [on the =]
... because this syntax is not valid ...

so it is not possible to do test and assignment in C style?
... no it's not, see
http://www.python.org/doc/faq/general/#why-can-t-i-use-an-assignment-in-an-expression

how can I write this otherwise?
... is this so bad?...

attr=global_re.match(line)
if not attr:
break

... or, since you don't seem to be doing much with attr, you could just do

if not global_re.match(line):
break

... and get rid of all those distracting ()'s!
 
P

Paolo Pantaleo

2006/4/23 said:
So I tried this

if(not (attr=global_re.match(line)) ):
break

it says invalid syntax [on the =]
... because this syntax is not valid ...

so it is not possible to do test and assignment in C style?
... no it's not, see
http://www.python.org/doc/faq/general/#why-can-t-i-use-an-assignment-in-an-expression

how can I write this otherwise?
... is this so bad?...

attr=global_re.match(line)
if not attr:
break

... or, since you don't seem to be doing much with attr, you could just do

if not global_re.match(line):
break

... and get rid of all those distracting ()'s!

Thnx for the help,
actually the problme is not solved

i have [well I want to do...] something like:

if a=b():
do stuff with a
else if a=c():
do stuff with b
else:
do other stuff

well, two solutions are

a1=b()
a2=c()

if a1:
do stuff with a1
else if a2:
do stuff with a2
else:
do other stuff


the other is


if b():
a=b()
do stuff with a
else if c():
a=c()
do stuff with b
else:
do other stuff

Even if none is exactly the same about:
* the number of times the b() and c() functions are executed
* the final value of a

I think the right one is:

a=b()
if a:
do stuff with a
else:
a=c()
if a=c():
do stuff with b
else:
do other stuff


PAolo
 
B

bruno at modulix

Paolo Pantaleo wrote:
(snip)
Thnx for the help,
actually the problme is not solved

i have [well I want to do...] something like:

if a=b():
do stuff with a
else if a=c():
do stuff with b

where does this 'b' come from ?
else:
do other stuff

well, two solutions are

a1=b()
a2=c()

if a1:
do stuff with a1
else if a2:
do stuff with a2
else:
do other stuff

if the call to b() returns a non-false value, the call to c() is useless.
the other is


if b():
a=b()
do stuff with a
else if c():
a=c()
do stuff with b
else:
do other stuff

You still have useless function calls.
Even if none is exactly the same about:
* the number of times the b() and c() functions are executed
* the final value of a

I think the right one is:

a=b()
if a:
do stuff with a
else:
a=c()
if a=c():
do stuff with b
else:
do other stuff

Almost :

a = b()
if a:
do_stuff_with_b(a)
else:
a = c()
if a:
do_stuff_with_c(a)
else:
do_other_stuff()


Now there are probably better ways to write this, but this would require
more knowledge about your real code.
 
D

Duncan Booth

bruno said:
Almost :

a = b()
if a:
do_stuff_with_b(a)
else:
a = c()
if a:
do_stuff_with_c(a)
else:
do_other_stuff()


Now there are probably better ways to write this, but this would require
more knowledge about your real code.

if there are more than a couple of options you can generalise code such as
this to use a for loop:

for guard, action in [
(b, do_stuff_with_b),
(c, do_stuff_with_c),
]:
if guard():
action(a)
break
else:
do_other_stuff()
 

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

No members online now.

Forum statistics

Threads
473,794
Messages
2,569,641
Members
45,353
Latest member
RogerDoger

Latest Threads

Top