How to get a reference of the 'owner' class to which a method belongs in Python 3.X?

Peter Otten __peter__ at web.de
Sat Mar 17 05:25:06 EDT 2012


Cosmia Luna wrote:

> I'm porting my existing work to Python 3.X, but...
> 
> class Foo:
>     def bar(self):
>         pass
> 
> mthd = Foo.bar
> 
> assert mthd.im_class is Foo # this does not work in py3k
> 
> So, how can I get a reference to Foo? This is important when writing
> decorators, the only way I can think out is:
> 
> class Foo:
>     def bar(self):
>         'Foo' # manually declare the owner class
>         pass
> 
> mthd = Foo.bar
> 
> assert mthd.__globals__[mthd.__doc__] is Foo # this works
> 
> class Child(Foo):
>     def bar(self):
>         'Child' # I have to override all method defined by bar but do
>         nothing pass
> 
> child_mthd = Child.bar
> 
> assert child_mthd.__globals__[child_mthd.__doc__] is Child # this works
> 
> But the code above is quite ugly and abuses the __doc__. Is there any
> equivalent in py3k of im_class?

class set_class:
    def __init__(self, method):
        self.method = method
    def __get__(self, instance, class_):
        if instance is None:
            method = self.method
            def f(*args, **kw):
                return method(*args, **kw)
            f.im_class = class_
            f.__name__ = method.__name__
            return f
        return self.method.__get__(instance, class_)
    
class Foo:
    def __init__(self, name):
        self.name = name
    @set_class
    def bar(self):
        print("%s says hello from bar()" % self)
    def __str__(self):
        return self.name

class Bar(Foo):
    pass

assert Foo.bar.im_class is Foo
assert Bar.bar.im_class is Bar

Foo("Fred").bar()
Foo.bar(Foo("Fred"))

Bar("Fred").bar()
Bar.bar(Bar("Fred"))

The cleaner approach is probably:

Rule('/<action>/', endpoint=(RootController, RootController.otheraction))
...
Controller, method = endpoint
controller = Controller(Request(environ))
...
method(controller)





More information about the Python-list mailing list