Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Is there way to determine which class a method is bound to?

Reply
Thread Tools

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

 
 
Victor Ng
Guest
Posts: n/a
 
      02-25-2005
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
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      02-25-2005
Victor Ng wrote:

> 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


>>> import inspect
>>> class Foo(object):

.... def foo(self): pass
....
>>> class Bar(Foo):

.... def bar(self): pass
....
>>> def get_imp_class(method):

.... 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



 
Reply With Quote
 
 
 
 
Victor Ng
Guest
Posts: n/a
 
      02-25-2005
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


On Fri, 25 Feb 2005 16:29:25 +0100, Peter Otten <(E-Mail Removed)> wrote:
> Victor Ng wrote:
>
> > 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

>
> >>> import inspect
> >>> class Foo(object):

> ... def foo(self): pass
> ...
> >>> class Bar(Foo):

> ... def bar(self): pass
> ...
> >>> def get_imp_class(method):

> ... 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
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>

 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      02-25-2005
Peter Otten wrote:

>>>> import inspect
>>>> class Foo(object):

> ...*****def*foo(self):*pass
> ...
>>>> class Bar(Foo):

> ...*****def*bar(self):*pass
> ...
>>>> def get_imp_class(method):

> ...*****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:

>>> def another(self): pass

....
>>> Foo.alpha = another
>>> Bar.beta = another
>>> get_imp_class(Bar.alpha)

<class '__main__.Foo'>
>>> get_imp_class(Bar.beta)

<class '__main__.Foo'>

A name check won't help either:

>>> Foo.alpha.__name__

'another'

Peter

 
Reply With Quote
 
Roy Smith
Guest
Posts: n/a
 
      02-25-2005
In article <(E-Mail Removed)>,
Victor Ng <(E-Mail Removed)> wrote:
>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
------

kinglay$ ./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.

 
Reply With Quote
 
percivall@gmail.com
Guest
Posts: n/a
 
      02-25-2005
Another way is to make a simple metaclass, setting an attribute (like
defining_class, or something) on each function object in the class
dictionary.

 
Reply With Quote
 
Victor Ng
Guest
Posts: n/a
 
      03-04-2005
So I went digging through the documentation more and found the following:

http://docs.python.org/ref/types.html

There's a section titled "User-defined methods" which covers all the
im_self, im_class attributes and what they are responsible for.

vic

On 25 Feb 2005 10:42:06 -0800, http://www.velocityreviews.com/forums/(E-Mail Removed) <(E-Mail Removed)> wrote:
> Another way is to make a simple metaclass, setting an attribute (like
> defining_class, or something) on each function object in the class
> dictionary.
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



--
---
"Never attribute to malice that which can be adequately explained by
stupidity." - Hanlon's Razor
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Lower bound & Upper bound sunil panda Java 9 10-07-2008 08:32 PM
how to call method of the class which contains a pointer to other class method? Pawel_Iks C++ 3 07-31-2007 06:30 AM
Is there any way to determine which control has the focus in the codebehind? José Joye ASP .Net 2 06-24-2006 11:59 AM
Determine if object is a Bound or Unbound method Farshid Lashkari Python 2 07-28-2005 08:41 PM
nuby: determine method passed and determine the receiver that received the method Peña, Botp Ruby 1 01-24-2004 07:51 PM



Advertisments