Re: test assignmet problem

2006-04-25 Thread 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.

-- 
bruno desthuilliers
python -c print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in '[EMAIL PROTECTED]'.split('@')])
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: test assignmet problem

2006-04-25 Thread Duncan Booth
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()
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: test assignmet problem

2006-04-24 Thread Paolo Pantaleo
2006/4/23, Paul McGuire [EMAIL PROTECTED]:

 Paolo Pantaleo [EMAIL PROTECTED] wrote in message
 news:[EMAIL PROTECTED]
 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!


 --
 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
-- 
http://mail.python.org/mailman/listinfo/python-list


test assignmet problem

2006-04-23 Thread 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
--
if you have a minute to spend please visit my photogrphy site:
http://mypic.co.nr
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: test assignmet problem

2006-04-23 Thread Duncan Booth
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.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: test assignmet problem

2006-04-23 Thread Paul McGuire

Paolo Pantaleo [EMAIL PROTECTED] wrote in message
news:[EMAIL PROTECTED]
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!


-- 
http://mail.python.org/mailman/listinfo/python-list