Another of those "is" issues.

Tim Wintle tim.wintle at
Fri Mar 20 19:37:11 CET 2009

On Fri, 2009-03-20 at 11:20 -0700, Emanuele D'Arrigo wrote:
> >>> def aFunction():
> ...     pass
> ...
> >>> f = aFunction
> >>> f is aFunction
> True   <--- Ok, this seems reasonable. Nevertheless, I suspect I
> shouldn't quite rely on it.

You can rely on this in the above - you've just assigned the name "f" to
the same object as "aFunction"

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

I believe that c.myMethod is actually a new object that's similar to
lambda self,*args,**kwargs: MyClass.myMethod(self,*args,**kwargs).


The MyClass *instance* has checked if there is an object
self.__dict__["myMethod"], and when it hasn't found it it creates a new
function which wraps the call to the base class up correctly. It's more
complicated than this though because you see the same behaviour when
checking the method on the class definition.

Seem to remember Raymond Hettinger pointing to the code that does this
as part of his "descriptor tutorial" talk at europython - but I can't
find the slides to reference.

hope that helps,

Tim Wintle

More information about the Python-list mailing list