Python:检查方法是否是静态的
假设以下类定义:
class A:
def f(self):
return 'this is f'
@staticmethod
def g():
return 'this is g'
a = A()
因此 f 是普通方法,g 是静态方法。
现在,我如何检查函数对象 af 和 ag 是否是静态的? Python 中有“isstatic”函数吗?
我必须知道这一点,因为我有包含许多不同函数(方法)对象的列表,并且要调用它们,我必须知道它们是否期望“self”作为参数。
assume following class definition:
class A:
def f(self):
return 'this is f'
@staticmethod
def g():
return 'this is g'
a = A()
So f is a normal method and g is a static method.
Now, how can I check if the funcion objects a.f and a.g are static or not? Is there a "isstatic" funcion in Python?
I have to know this because I have lists containing many different function (method) objects, and to call them I have to know if they are expecting "self" as a parameter or not.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
让我们尝试一下:
看起来您可以使用 types.FunctionType 来区分静态方法。
Lets experiment a bit:
So it looks like you can use
types.FunctionType
to distinguish static methods.你的方法对我来说似乎有点缺陷,但你可以检查类属性:(
在Python 2.7中):
或在Python 3.x中检查实例属性
Your approach seems a bit flawed to me, but you can check class attributes:
(in Python 2.7):
or instance attributes in Python 3.x
为了补充这里的答案,在 Python 3 中最好的方法如下:
我们使用
getattr_static
而不是getattr
,因为getattr
将检索绑定方法或函数,而不是staticmethod
类对象。您可以对classmethod
类型和property
进行类似的检查(例如使用@property
装饰器定义的属性)从技术上讲,任何方法都可以用作“静态”方法,只要它们在类本身上调用,所以请记住这一点。例如,这将工作得很好:
该示例不适用于
Test
的实例,因为该方法将绑定到实例并被称为Test。改为测试(自我)
。在某些情况下,实例和类方法也可以用作静态方法,只要正确处理第一个参数即可。
也许另一种罕见的情况是静态方法也绑定到类或实例。例如:
虽然从技术上讲它是一个
静态方法
,但它的行为实际上就像一个类方法
。因此,在您的自省中,您可以考虑检查 __self__ (在 __func__ 上递归)以查看该方法是否绑定到类或实例。对于那些进行更深入内省的人来说,另一个最后的警告是静态方法可以在不同的类中定义。
__qualname__
将显示静态方法是否在其附加的类中定义。To supplement the answers here, in Python 3 the best way is like so:
We use
getattr_static
rather thangetattr
, sincegetattr
will retrieve the bound method or function, not thestaticmethod
class object. You can do a similar check forclassmethod
types andproperty
's (e.g. attributes defined using the@property
decorator)Technically, any method can be used as "static" methods, so long as they are called on the class itself, so just keep that in mind. For example, this will work perfectly fine:
That example will not work with instances of
Test
, since the method will be bound to the instance and called asTest.test(self)
instead.Instance and class methods can be used as static methods as well in some cases, so long as the first arg is handled properly.
Perhaps another rare case is a
staticmethod
that is also bound to a class or instance. For example:Though technically it is a
staticmethod
, it is really behaving like aclassmethod
. So in your introspection, you may consider checking the__self__
(recursively on__func__
) to see if the method is bound to a class or instance.Another final caution for those doing more in depth introspection is that a
staticmethod
could be defined in a different class. The__qualname__
will reveal whether the static method was defined in the class it is attached to or not.我碰巧有一个模块可以解决这个问题。它是Python2/3 兼容解决方案。并且它允许使用从父类继承的方法进行测试。
另外,该模块还可以测试:
例如:
以下是仅静态方法的解决方案。但我建议使用该模块发布在这里。
I happens to have a module to solve this. And it's Python2/3 compatible solution. And it allows to test with method inherit from parent class.
Plus, this module can also test:
For example:
Here's the solution for staticmethod only. But I recommend to use the module posted here.
何苦呢?您可以像调用 f 一样调用 g:
Why bother? You can just call g like you call f:
python 中无法检查方法是否是静态的,
只能通过方法对象
来检查。这是我的实现。There is no way in python to check if the method is static or not,
only with having the method object
. this is my implementation.