descriptor dilemma
Peter Otten
__peter__ at web.de
Wed May 4 12:35:49 EDT 2005
john wrote:
> Why do c.f and C.__dict__['f'].__get__(c,C) compare as equal under ==
> but not under *is* ?
These variations are equivalent. Every attribute access gives you a new
bound method:
>>> class C(object):
... def f(self): pass
...
>>> c = C()
>>> c.f is c.f
False
>>> c.f == c.f
True
'a is b' is true only when the names a and b are bound to the same object.
For bound methods, c.f is c.f == True would mean that Python would have to
cache them, and I don't see a potential advantage of that.
'a == b' on the other hand does what the programmer specifies:
>>> class Str(str):
... def __eq__(self, other): return True
...
>>> Str("black") == Str("blue")
True
It makes sense for bound methods to compare equal if they refer to the same
function and instance (though I don't know if the class is checked, too).
Peter
More information about the Python-list
mailing list