re.search when used within an if/else fails

Steven D'Aprano steve+comp.lang.python at pearwood.info
Mon Nov 19 20:24:54 EST 2012


On Mon, 19 Nov 2012 15:43:10 -0800, 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 )


Your code is mangled to the point of unreadability. 

Please resend, and make sure you send it as PLAIN TEXT and not as HTML 
("rich text"), since many mail programs feel that they are allowed to 
arbitrarily rewrap HTML text however they like.

Preferably simplify your example to the simplest example that we can run:

http://sscce.org/

Being able to run it means you shouldn't put line numbers on the left. If 
you must draw our attention to a specific line, use a comment. This isn't 
1975 and we're not programming in BASIC.

# BAD don't do this:
350  do_this(x)
351  do_that(y, z)
352  if something:
353      do_something_else(x, y, z)


# GOOD do this:
do_this(x)
do_that(y, z)
if something:   # LINE 352
    do_something_else(x, y, z)




> 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...

The code you show doesn't actually have an `else` clause, which might 
explain why it doesn't get executed.

By the way, you should not write "if something == None", always use "if 
something is None" or "is not None". Technically, there are some 
exceptions but if you have to ask what they are, you don't need to know 
*wink*



-- 
Steven



More information about the Python-list mailing list