【发布时间】:2012-01-14 14:04:09
【问题描述】:
给定一个带有函数或方法f 的类C,我使用inspect.ismethod(obj.f)(其中obj 是C 的一个实例)来确定f 是否是绑定方法。有没有办法在类级别直接做同样的事情(不创建对象)?
inspect.ismmethod 不能这样工作:
class C(object):
@staticmethod
def st(x):
pass
def me(self):
pass
obj = C()
结果(在 Python 3 中):
>>> inspect.ismethod(C.st)
False
>>> inspect.ismethod(C.me)
False
>>> inspect.ismethod(obj.st)
False
>>> inspect.ismethod(obj.me)
True
我想我需要检查函数/方法是否是类的成员而不是静态的,但我无法轻松做到这一点。我想这可以使用classify_class_attrs 来完成,如下所示
How would you determine where each property and method of a Python class is defined?
但我希望有另一种更直接的方法。
【问题讨论】:
标签: python function methods introspection