37 lines
1.1 KiB
Plaintext
37 lines
1.1 KiB
Plaintext
From: fredrik at pythonware.com (Fredrik Lundh)
|
|
Date: Thu, 22 Apr 1999 12:13:39 GMT
|
|
Subject: ``if t'' vs ``if t is not None''
|
|
References: <7fn1vb$rbo$1@news.glas.net>
|
|
Message-ID: <00e601be8cb9$92a7ee30$f29b12c2@pythonware.com>
|
|
X-UID: 1246
|
|
|
|
<jno at 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>
|
|
|
|
|
|
|
|
|
|
|