re.search when used within an if/else fails

Ian Kelly ian.g.kelly at gmail.com
Tue Nov 20 20:39:10 CET 2012


On Tue, Nov 20, 2012 at 12:37 PM, Ian Kelly <ian.g.kelly at gmail.com> wrote:
> On Tue, Nov 20, 2012 at 12:09 PM, Kevin T <kevinintx at gmail.com> wrote:
>> #if re.search( "rsrvd", sigName ) :   #version a
>> #if re.search( "rsrvd", sigName ) == None :   #version b
>> if re.search( "rsrvd", sigName ) is None :   #version bb
>>    print sigName
>>    newVal = "%s%s" % ('1'*signal['bits'] , newVal )
>> #else:                                 #version c
>> if re.search( "rsrvd", sigName ) != None :   #version d
>>    print sigName
>>    newVal = "%s%s" % ( '0'*signal['bits'],> newVal )
>>
>> i can use either version a/b the else clause (version c) will not execute.
>> fortunately,  with version bb, the else clause will execute!!
>
> There must be some other difference in your testing.  I don't have
> Python 2.4 available, but I tried your version a in both Python 2.3
> and 2.5 using made-up values for sigName, and the else clause is
> executed in both.

It should be noted, however, that version a is the logical *inverse*
of both b and bb.  With version a, you're testing for the logical
truth of the re.search result; it will be true if it is *not* None.
With the other two you are testing that the result *is* None.


More information about the Python-list mailing list