Scott David Daniels wrote: > 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
Well of course it will be false... your testing two different strings! And the resulting slice creates a third. Try: ABC = 'abc' value = ABC if value is ABC: # Test if it is the same object pass In the previous discussion I was comparing using a string as an alternative to using None as a "flag" object. Not as a value to be calculated. And just to be clear, I'm not disagreeing with you. Yes, you can have problems with string comparisons if you create a copy instead of pointing a name to the object like you did above. Something to be aware of. To avoid that you either need to define the flag string as a global name or use it strictly in the local scope it's defined in. Python will also sometimes reuse strings as an optimization instead of creating a second string if they are equal. Something else to be aware of. So I'm not suggesting it is a good idea to use strings in place of None. But I still wonder why bool and other object comparisons are slightly slower than string comparisons. (?) Cheers, Ron > 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 > [EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-list