Can I find out (dynamically) where a method is defined?

Maric Michaud maric at aristote.info
Mon Jun 9 18:01:25 CEST 2008


Le Monday 09 June 2008 17:28:45 allendowney at gmail.com, vous avez écrit :
> So, is there any way to inspect a method to see where (in what class)
> it
> is defined?

In [56]: class a(object) :
    def a() : return
    def b() :  return
   ....:
   ....:

In [59]: class b(a) :
    def b() : return
    def c() : return
   ....:
   ....:

In [62]: i=b()

In [63]: for m in 'a', 'b', 'c' :
   ....:     print [ t for t in type(i).mro() if m in dir(t) ]
   ....:
   ....:
[<class '__main__.a'>]
[<class '__main__.b'>, <class '__main__.a'>]
[<class '__main__.b'>]

mro stands for "method resolution order", check the reference on this 
and "super", it worths the effort.

-- 
_____________

Maric Michaud
_____________

Aristote - www.aristote.info
3 place des tapis
69004 Lyon
Tel: +33 4 26 88 00 97
Mobile: +33 6 32 77 00 21



More information about the Python-list mailing list