[Tutor] getting a result from an if condition

Kent Johnson kent37 at tds.net
Thu Oct 4 18:01:03 CEST 2007


Tino Dai wrote:
> Hi Everybody,
> 
>      I'm having some problems with get an if block to work.
> 
> import re
> 
> regex=re.compile('(some expression)')
> 
> # What I'm trying to get to work
> if (m=regex.search('some long string')):
>       print m.groups()
> 
> - The thing that isn't working is the m=regex .... in the if line. Is this
> possible in python?

No. Assignment in Python is a statement, not an expression. It does not 
have a value and it can't be used in a conditional. Use

m=regex.search('some long string')
if (m):
       print m.groups()

Kents


More information about the Tutor mailing list