Mailing List Archive

``if t'' vs ``if t is not None''
hi there,

i faced a VERY strange behaviour of if-test operation!

``if t'' != ``if t is not None''
where ``t'' is None or a class instance

i was writing a threader for a news reader.
and found occasional hangs of a routine which builds the sequence of message
numbers for "read next" operation.
digging deeper brought me strange results: replacing ``if t'' with
``if t is not None'' speeded up the things dramatically!

the call contecst:

for x in self.keys() :
m, t = self.mesg_list[ x ]
self.Sequence.append( m.num )
if t is not None :
t.buildSequence(i+1)
self.Sequence.extend( t.Sequence )
del t.Sequence ; t.Sequence = None # trash extra data

can anyone explain me such a phenomenon???

--
SY, [ mailto:jno@glas.apc.org ]
jno (PRIVATE PERSON) [ http://www.glasnet.ru/~jno ]
a GlasNet techie [ http://www.aviation.ru/ ]
If God meant man to fly, He'd have given him more money.
``if t'' vs ``if t is not None'' [ In reply to ]
<jno@glasnet.ru> wrote:
> i faced a VERY strange behaviour of if-test operation!
>
> ``if t'' != ``if t is not None''
> where ``t'' is None or a class instance
>
> i was writing a threader for a news reader.
> and found occasional hangs of a routine which builds the sequence of message
> numbers for "read next" operation.
> digging deeper brought me strange results: replacing ``if t'' with
> ``if t is not None'' speeded up the things dramatically!

"if t" evaluates "t" by calling it's __nonzero__ or __len__ methods
(see http://www.python.org/doc/ref/customization.html for
details).

"if t is not None" compares t's object identity (a pointer) with
the object identity for None (another pointer).

in other words, using "is" tests are always faster. how much
faster depends on how much work you do in your __len__
(or __nonzero__) method...

</F>