[SciPy-User] check where method is defined

Skipper Seabold jsseabold at gmail.com
Thu Jun 16 11:18:20 EDT 2011


On Thu, Jun 16, 2011 at 10:59 AM,  <josef.pktd at gmail.com> wrote:
> What's the best way to check in which class or super class a method is defined ?
>
> I would like to write some tests that only apply if the specific
> distribution class defines the method.
>
> For example: Is _sf defined in the specific distribution class or is
> it the generic implementation in the superclass, rv_continuous
>
>>>> stats.gengamma._sf
> <bound method gengamma_gen._sf of
> <scipy.stats.distributions.gengamma_gen object at 0x05679470>>
>

Something like this?

import inspect

def get_class_that_defined_method(meth):
    obj = meth.im_self
    for cls in inspect.getmro(meth.im_class):
        if meth.__name__ in cls.__dict__: return cls
    return None

http://stackoverflow.com/questions/961048/get-class-that-defined-method-in-python

Skipper



More information about the SciPy-User mailing list