Another of those "is" issues.

Emanuele D'Arrigo manu3d at gmail.com
Fri Mar 20 14:20:27 EDT 2009


Hi everybody,

I was unit testing some code today and I eventually stumbled on one of
those "is" issues quickly solved replacing the "is" with "==". Still,
I don't quite see the sense of why these two cases are different:

>>> def aFunction():
...     pass
...
>>> f = aFunction
>>> f is aFunction
True   <--- Ok, this seems reasonable. Nevertheless, I suspect I
shouldn't quite rely on it.

>>> class MyClass(object):
...     def myMethod(self):
...         pass
...
>>> c = MyClass()
>>> m = c.myMethod
>>> m is c.myMethod
False  <--- What? Why is that?

In my mind I was expecting that when the method is assigned to "m" all
that it happens is that its address is assigned to the name "m" so
that effectively the same address is now pointed to by two names, like
in the function case. I googled around for some hint but I wouldn't
exactly say I'm clear on the issue just yet...

Can anybody shed some light? Or point to a resource to look at? Or
what's the bit of python's source code that is responsible for dealing
with those assignments?

Manu





More information about the Python-list mailing list