[SciPy-User] check where method is defined
josef.pktd at gmail.com
josef.pktd at gmail.com
Thu Jun 16 11:45:25 EDT 2011
On Thu, Jun 16, 2011 at 11:18 AM, Skipper Seabold <jsseabold at gmail.com> wrote:
> 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
Thanks, looks good
>>> get_class_that_defined_method(stats.gengamma._sf)
<class 'scipy.stats.distributions.rv_continuous'>
>>> get_class_that_defined_method(stats.gengamma._pdf)
<class 'scipy.stats.distributions.gengamma_gen'>
>>> get_class_that_defined_method(stats.gengamma.rvs)
<class 'scipy.stats.distributions.rv_generic'>
>>> get_class_that_defined_method(stats.gengamma.rvs) is stats.distributions.rv_generic
True
>>> get_class_that_defined_method(stats.gengamma.rvs) is stats.distributions.gengamma_gen
False
>>> get_class_that_defined_method(stats.gengamma.rvs) is stats.gengamma.__class__
False
>>> get_class_that_defined_method(stats.gengamma._pdf) is stats.gengamma.__class__
True
Josef
>
> Skipper
> _______________________________________________
> SciPy-User mailing list
> SciPy-User at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
More information about the SciPy-User
mailing list