Is there way to determine which class a method is bound to?

V

Victor Ng

I'm doing some evil things in Python and I would find it useful to
determine which class a method is bound to when I'm given a method
pointer.

For example:

class Foo(object):
def somemeth(self):
return 42

class Bar(Foo):
def othermethod(self):
return 42


Is there some way I can have something like :

findClass(Bar.somemeth)

that would return the 'Foo' class, and

findClass(Bar.othermethod)

would return the 'Bar' class?

vic
 
P

Peter Otten

Victor said:
I'm doing some evil things in Python and I would find it useful to
determine which class a method is bound to when I'm given a method
pointer.

For example:

class Foo(object):
def somemeth(self):
return 42

class Bar(Foo):
def othermethod(self):
return 42


Is there some way I can have something like :

findClass(Bar.somemeth)

that would return the 'Foo' class, and

findClass(Bar.othermethod)

would return the 'Bar' class?

vic
.... def foo(self): pass
........ def bar(self): pass
........ return [t for t in inspect.classify_class_attrs(method.im_class) if
t[-1] is method.im_func][0][2]
....
[get_imp_class(m) for m in [Bar().foo, Bar().bar, Bar.foo, Bar.bar]]
[<class '__main__.Foo'>, <class '__main__.Bar'>, <class '__main__.Foo'>,
<class '__main__.Bar'>]

but with this approach you will get into trouble as soon as you are using
the same function to define multiple methods. There may be something in the
inspect module more apt to solve the problem -- getmro() perhaps?

Peter
 
V

Victor Ng

Awesome! I didn't see the getmro function in inspect - that'll do the
trick for me. I should be able to just look up the methodname in each
of the class's __dict__ attributes.

vic


Victor said:
I'm doing some evil things in Python and I would find it useful to
determine which class a method is bound to when I'm given a method
pointer.

For example:

class Foo(object):
def somemeth(self):
return 42

class Bar(Foo):
def othermethod(self):
return 42


Is there some way I can have something like :

findClass(Bar.somemeth)

that would return the 'Foo' class, and

findClass(Bar.othermethod)

would return the 'Bar' class?

vic
... def foo(self): pass
...... def bar(self): pass
...... return [t for t in inspect.classify_class_attrs(method.im_class) if
t[-1] is method.im_func][0][2]
...
[get_imp_class(m) for m in [Bar().foo, Bar().bar, Bar.foo, Bar.bar]]
[<class '__main__.Foo'>, <class '__main__.Bar'>, <class '__main__.Foo'>,
<class '__main__.Bar'>]

but with this approach you will get into trouble as soon as you are using
the same function to define multiple methods. There may be something in the
inspect module more apt to solve the problem -- getmro() perhaps?

Peter
 
P

Peter Otten

Peter said:
...     def foo(self): pass
... ...     def bar(self): pass
...
...     return [t for t in inspect.classify_class_attrs(method.im_class)
if t[-1] is method.im_func][0][2]
...
[get_imp_class(m) for m in [Bar().foo, Bar().bar, Bar.foo, Bar.bar]]
[<class '__main__.Foo'>, <class '__main__.Bar'>, <class '__main__.Foo'>,
<class '__main__.Bar'>]

but with this approach you will get into trouble as soon as you are using
the same function to define multiple methods. There may be something in

I think it might be better to demonstrate the problem than just to describe
it:
<class '__main__.Foo'>

A name check won't help either:
'another'

Peter
 
R

Roy Smith

I'm doing some evil things in Python and I would find it useful to
determine which class a method is bound to when I'm given a method
pointer.

I don't know where (or if) it's documented, but im_class seems to give
you what you want.

------
class Foo(object):
def x(self):
return 42

f = Foo()
print f.x.im_class
------

king:play$ ./x.py
<class '__main__.Foo'>

I have no idea why it's not __imclass__ or some such, but poking
around with dir() is a great way to explore little nooks and crannies
like this. I just printed dir(Foo().x) and tried stuff that looked
interesting until I found what I (you) wanted.
 
P

percivall

Another way is to make a simple metaclass, setting an attribute (like
defining_class, or something) on each function object in the class
dictionary.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top