如何推断@staticmethod属于哪个类?

4 投票
2 回答
1061 浏览
提问于 2025-04-15 12:02

我正在尝试实现一个叫做 infer_class 的函数,这个函数的作用是根据一个方法,找出这个方法属于哪个类。

到目前为止,我的代码大致是这样的:

import inspect

def infer_class(f):
    if inspect.ismethod(f):
        return f.im_self if f.im_class == type else f.im_class
    # elif ... what about staticmethod-s?
    else:
        raise TypeError("Can't infer the class of %r" % f)

但是这个代码对 @staticmethod 的方法不管用,因为我还没有找到合适的方法来解决这个问题。

有没有什么建议呢?

下面是 infer_class 的实际使用情况:

>>> class Wolf(object):
...     @classmethod
...     def huff(cls, a, b, c):
...         pass
...     def snarl(self):
...         pass
...     @staticmethod
...     def puff(k,l, m):
...         pass
... 
>>> print infer_class(Wolf.huff)
<class '__main__.Wolf'>
>>> print infer_class(Wolf().huff)
<class '__main__.Wolf'>
>>> print infer_class(Wolf.snarl)
<class '__main__.Wolf'>
>>> print infer_class(Wolf().snarl)
<class '__main__.Wolf'>
>>> print infer_class(Wolf.puff)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 6, in infer_class
TypeError: Can't infer the class of <function puff at ...>

2 个回答

3

我很难真的去推荐这个,但至少在简单的情况下,它似乎是有效的:

import inspect

def crack_staticmethod(sm):
    """
    Returns (class, attribute name) for `sm` if `sm` is a
    @staticmethod.
    """
    mod = inspect.getmodule(sm)
    for classname in dir(mod):
        cls = getattr(mod, classname, None)
        if cls is not None:
            try:
                ca = inspect.classify_class_attrs(cls)
                for attribute in ca:
                    o = attribute.object
                    if isinstance(o, staticmethod) and getattr(cls, sm.__name__) == sm:
                        return (cls, sm.__name__)
            except AttributeError:
                pass
3

这是因为静态方法其实并不是传统意义上的方法。静态方法的描述符会直接返回原始的函数,没有办法通过它来获取访问这个函数的类。不过,实际上使用静态方法来定义方法并没有什么特别的必要,建议你总是使用类方法。

我发现静态方法唯一的用处就是把函数对象当作类的属性来存储,而不让它们变成方法。

撰写回答