super, decorators and gettattribute

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sun Jan 13 00:56:21 CET 2008


On Sat, 12 Jan 2008 15:47:05 -0500, Mike Meyer wrote:

> There's an apparently common bug here: you don't want to pass super
> self.__class__, but the class that the method is bound to.

Given an instance method, is it possible to easily determine what class 
it is defined in?

I thought the im_class attribute might do it, but it apparently just 
points to self.__class__.

>>> class Foo(object):
...     def foo(self):
...             pass
...
>>> class Bar(Foo):
...     def bar(self):
...             pass
... 
>>> Bar().bar.im_class  # expecting Bar
<class '__main__.Bar'>
>>> Bar().foo.im_class  # hoping for Foo
<class '__main__.Bar'>



-- 
Steven



More information about the Python-list mailing list