re.search when used within an if/else fails

MRAB python at mrabarnett.plus.com
Mon Nov 19 20:21:54 EST 2012


On 2012-11-19 23:43, Kevin T wrote:
> python version 2.4.3, yes i know that it is old.  getting the sysadmin to update the OS requires a first born.
>
> with the following code..
>                  for signal in register['signals'] :
>
> 351                   sigName = signal['functionName']
> 352                   if re.search( "rsrvd", sigName ) == None :
> 353                      print sigName
> 354                      newVal = "%s%s" % ( '1'*signal['bits'] , newVal ) #prepend 0's
> 355                   if re.search( "rsrvd", sigName ) != None :
> 356                      print sigName
> 357                      newVal = "%s%s" % ( '0'*signal['bits'], newVal )
>
> regardless of how i code line 352, i can not EVER use an else clause with it.  if i use an else clause, the else will NEVER get executed...
>
> has any one experienced anything like this behavior?  any suggestions?  the above code works but... why should i have to code it like this?
>
Have you checked the indentation? There may be a mixture of tabs and spaces.

A couple of points:

1. You should be using "is None" and "is not None" instead of "== None" 
and "!= None".

2. You don't need to use regex. Use "rsrvd" in sigName and "rsrvd" not 
in sigName.




More information about the Python-list mailing list