Use cases for del

Scott David Daniels Scott.Daniels at Acm.Org
Sat Jul 9 19:15:05 EDT 2005


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 at Acm.Org



More information about the Python-list mailing list