[prev in list] [next in list] [prev in thread] [next in thread] 

List:       python-list
Subject:    Re: Use cases for del
From:       Scott David Daniels <Scott.Daniels () Acm ! Org>
Date:       2005-07-09 23:15:05
Message-ID: 42d0529a$1 () nntp0 ! pdx ! net
[Download RAW message or body]

Ron Adam wrote:
> George Sakkis wrote:
> 
>> I get:
>>
>> None: 0.549999952316
>> String: 0.498000144958
>> is None: 0.450000047684
> 
> 
> What do yo get for "name is 'string'" expressions?

     >>> 'abc' is 'abcd'[:3]
     False

You need to test for equality (==), not identity (is) when
equal things may be distinct.  This is true for floats, strings,
and most things which are not identity-based (None, basic classes).
This is also true for longs and most ints (an optimization that makes
"small" ints use a single identity can lead you to a mistaken belief
that equal integers are always identical.

     >>> (12345 + 45678) is (12345 + 45678)
     False

'is' tests for identity match.  "a is b" is roughly equivalent to
"id(a) == id(b)".  In fact an optimization inside string comparisons
is the C equivalent of "return (id(a) == id(b) of len(a) == len(b)
and <elements match>)

--Scott David Daniels
Scott.Daniels@Acm.Org
-- 
http://mail.python.org/mailman/listinfo/python-list
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic