easy question on parsing python: "is not None"
thomas at jollans.com
Sat Aug 7 14:28:48 CEST 2010
On 08/07/2010 09:44 AM, Gabriel Genellina wrote:
> En Sat, 07 Aug 2010 04:04:06 -0300, Stefan Schwarzer
> <sschwarzer at sschwarzer.net> escribió:
>> On 2010-08-07 00:28, Steven D'Aprano wrote:
>>> Actually, yes, equality is implemented with a short-cut
> that checks for
>>> identity first. That makes something like:
>> Oops, I didn't realize that the OP had mentioned the
>> identity check as an optimization in case the objects are
>> the same. I thought he was confusing the operator with `is`.
>>> s = "abc"*1000*1000*10
>>> s == s
>>> nice and quick, as Python can immediately recognise that a string is
>>> always equal to itself without having to walk the entire string
>>> each character with itself.
>> Yes, that definitely makes sense. I guess I would have
>> implemented it this way as well. :)
> For strings and other internal types this optimization certainly makes
> sense. For user-defined types it gets in the way and prevents defining
> an object such x==x is False (like NANs).
That's probably why this optimisation doesn't exist for user-defined types:
Python 3.1.2 (release31-maint, Jul 8 2010, 09:18:08)
[GCC 4.4.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> class A:
... def __eq__(self, x):
... if self is x: return False
... else: return True
>>> A() == A()
>>> a = A()
>>> a == a
More information about the Python-list