Python: 检查方法是否为静态方法
假设有以下的类定义:
class A:
def f(self):
return 'this is f'
@staticmethod
def g():
return 'this is g'
a = A()
这里,f 是一个普通方法,而 g 是一个静态方法。
那么,我该如何检查函数对象 a.f 和 a.g 是否是静态的呢?在 Python 里有没有什么 "isstatic" 的函数?
我需要知道这个,因为我有一些列表,里面包含了很多不同的函数(方法)对象,而在调用它们的时候,我必须知道它们是否需要 "self" 作为参数。
5 个回答
14
我觉得你的方法有点问题,不过你可以查看类的属性:
(在Python 2.7中):
>>> type(A.f)
<type 'instancemethod'>
>>> type(A.g)
<type 'function'>
或者在Python 3.x中查看实例的属性
>>> a = A()
>>> type(a.f)
<type 'method'>
>>> type(a.g)
<type 'function'>
23
让我们来做一些实验:
>>> import types
>>> class A:
... def f(self):
... return 'this is f'
... @staticmethod
... def g():
... return 'this is g'
...
>>> a = A()
>>> a.f
<bound method A.f of <__main__.A instance at 0x800f21320>>
>>> a.g
<function g at 0x800eb28c0>
>>> isinstance(a.g, types.FunctionType)
True
>>> isinstance(a.f, types.FunctionType)
False
看起来你可以用 types.FunctionType
来区分静态方法。
5
我刚好有一个模块可以解决这个问题。而且这个解决方案兼容Python2和Python3。它允许你测试那些从父类继承的方法。
此外,这个模块还可以测试:
- 普通属性
- 属性风格的方法
- 普通方法
- 静态方法
- 类方法
举个例子:
class Base(object):
attribute = "attribute"
@property
def property_method(self):
return "property_method"
def regular_method(self):
return "regular_method"
@staticmethod
def static_method():
return "static_method"
@classmethod
def class_method(cls):
return "class_method"
class MyClass(Base):
pass
这里是仅针对静态方法的解决方案。不过我建议使用这个模块,可以在这里找到。
import inspect
def is_static_method(klass, attr, value=None):
"""Test if a value of a class is static method.
example::
class MyClass(object):
@staticmethod
def method():
...
:param klass: the class
:param attr: attribute name
:param value: attribute value
"""
if value is None:
value = getattr(klass, attr)
assert getattr(klass, attr) == value
for cls in inspect.getmro(klass):
if inspect.isroutine(value):
if attr in cls.__dict__:
bound_value = cls.__dict__[attr]
if isinstance(bound_value, staticmethod):
return True
return False