470,591 Members | 1,458 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,591 developers. It's quick & easy.

test assignmet problem

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
--
if you have a minute to spend please visit my photogrphy site:
http://mypic.co.nr
Apr 23 '06 #1
5 862
Paolo Pantaleo wrote:
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.
Apr 23 '06 #2

"Paolo Pantaleo" <pa***********@gmail.com> wrote in message
news:ma***************************************@pyt hon.org...
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/genera...-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!
Apr 23 '06 #3
2006/4/23, Paul McGuire <pt***@austin.rr._bogus_.com>:

"Paolo Pantaleo" <pa***********@gmail.com> wrote in message
news:ma***************************************@pyt hon.org...
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/genera...-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 justdo

if not global_re.match(line):
break

... and get rid of all those distracting ()'s!
--
http://mail.python.org/mailman/listinfo/python-list


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
--
if you have a minute to spend please visit my photogrphy site:
http://mypic.co.nr
Apr 24 '06 #4
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.

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Apr 25 '06 #5
bruno at modulix wrote:
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()
Apr 25 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Randell D. | last post: by
5 posts views Thread by Jan Roland Eriksson | last post: by
reply views Thread by Tim Haughton | last post: by
18 posts views Thread by Scott David Daniels | last post: by
8 posts views Thread by Vladimir S. Oka | last post: by
3 posts views Thread by Alex | last post: by
1 post views Thread by --== Alain ==-- | last post: by
176 posts views Thread by nw | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.