获取定义方法的类
如何获取在Python中定义方法的类?
我希望以下示例打印“__main__.FooClass
”:
class FooClass:
def foo_method(self):
print "foo"
class BarClass(FooClass):
pass
bar = BarClass()
print get_class_that_defined_method(bar.foo_method)
How can I get the class that defined a method in Python?
I'd want the following example to print "__main__.FooClass
":
class FooClass:
def foo_method(self):
print "foo"
class BarClass(FooClass):
pass
bar = BarClass()
print get_class_that_defined_method(bar.foo_method)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
我不知道为什么没有人提出这个问题,也不知道为什么当它慢得要命时,最上面的答案有 50 个赞成票,但你也可以执行以下操作:
对于 python 3,我相信这发生了变化,你需要看看进入
.__qualname__
。I don't know why no one has ever brought this up or why the top answer has 50 upvotes when it is slow as hell, but you can also do the following:
For python 3 I believe this changed and you'll need to look into
.__qualname__
.在 Python 3 中,如果您需要实际的类对象,您可以这样做:
如果该函数可能属于嵌套类,您将需要按如下方式进行迭代:
In Python 3, if you need the actual class object you can do:
If the function could belong to a nested class you would need to iterate as follows:
感谢 Sr2222 指出我错过了重点......
这是正确的方法,就像 Alex 的方法一样,但不需要导入任何内容。 不过,我不认为这是一种改进,除非继承类的层次结构很大,因为这种方法在找到定义类后就会停止,而不是像
getmro
那样返回整个继承。 如前所述,这是一种非常不太可能发生的情况。示例:
Alex 解决方案返回相同的结果。 只要可以使用 Alex 的方法,我就会使用它而不是这个。
Thanks Sr2222 for pointing out I was missing the point...
Here's the corrected approach which is just like Alex's but does not require to import anything. I don't think it's an improvement though, unless there's a huge hierarchy of inherited classes as this approach stops as soon as the defining class is found, instead of returning the whole inheritance as
getmro
does. As said, this is a very unlikely scenario.And the Example:
Alex solution returns the same results. As long as Alex approach can be used, I would use it instead of this one.
我发现 __qualname__ 在 Python3 中很有用。
我这样测试:
测试后,我在这里找到了另一个答案。
I found __qualname__ is useful in Python3.
I test it like that:
After my test, I found another answer here.
inspect._findclass
似乎适用于任何函数/方法。inspect._findclass
seems to be working fine for any function/method.Python 3
以非常简单的方式解决了这个问题:
str(bar.foo_method).split(" ", 3)[-2]
这给出了
'FooClass.foo_method'
分割点上分别获取类和函数名
Python 3
Solved it in a very simple way:
str(bar.foo_method).split(" ", 3)[-2]
This gives
'FooClass.foo_method'
Split on the dot to get the class and the function name separately
我尝试做类似的事情来检查基类中的存根方法是否已在子类中实现。 无论我尝试哪种方式,我都无法检测到中间类何时实际实现该方法(下面的 d.run_method() 情况)。
我最终通过设置一个方法 attribute 并稍后测试它的存在来做到这一点:
PS:这并不能直接回答问题......恕我直言,人们想知道哪个类定义了一个属性有两个主要原因:方法; 一种是在调试代码中指向某个类(例如在异常处理中),另一种是确定该方法是否已被重新实现(其中方法是由程序员实现的存根)。 这个答案以不同的方式解决了第二种情况。
I tried doing something similar to check if a stub method in a base class had been implemented or not in a subclass. Whichever way I tried I could not detect when an intermediate class was actually implementing the method (
d.run_method()
case below).I finally did it by setting a method attribute and testing its presence later:
P.S.: This doesn't answer directly the question... IMHO there are two major reasons one would want to know which class defined a method; one is to point fingers at a class in debug code (such as in exception handling), and another is to determine if the method has been re-implemented (where method is a stub meant to be implemented by the programmer). This answer solves that second case in a different way.
从Python 3.6开始,您就可以使用
__set_name__
作为描述符上的挂钩,如在此答案中所述复制此问题。Since Python 3.6, you have been able to use
__set_name__
as a hook on a descriptor, as described in this answer to a duplicate of this question.如果您收到此错误:
请尝试以下操作:
示例测试:
if you get this error:
try this:
sample test:
Python 3 的另一个解决方案:
输出:
Python 2.7 或 3:
Another solution for Python 3:
Output:
Python 2.7 or 3:
我们可以使用方法解析顺序或
mro()
从some_method
中查找SomeClass
的名称,它具有:输出:
这样我们就可以找到
some_method
所属类的名称,即使它是由SomeOtherClass
继承的:输出:
或所有具有
some_method
的类的名称(或some_other_method
):输出:
获取
str
中的__name__
:We can use method resolution order or
mro()
to find the name ofSomeClass
fromsome_method
it has:Outputs:
that way we can find the name of the class that
some_method
belongs to even if it's inherited bySomeOtherClass
:Outputs:
or the names of all the classes that have
some_method
(orsome_other_method
):Outputs:
To get the
__name__
s instr
ing:只需使用
__qualname__
属性ClassOrInstance.method.__qualname__
即可生成Class.method
字符串代码,使用
Python 3.8.8
进行测试code>下一步
如果您想检查代码中的实现位置,您可以执行
以下操作: 这里演示的是
class
而不是instance
。simply use the
__qualname__
attributeClassOrInstance.method.__qualname__
yields aClass.method
stringCode, tested with
Python 3.8.8
Next step
If you want to check for the place of implementation in the code, you can do
Here it is demonstrated for a
class
instead of aninstance
.