[英]How to reimplement Python's __qualname__ in Python 3.7? (with some minor adjustments)
The __qualname__
attribute is useful to me because it contextualizes functions; __qualname__
属性对我很有用,因为它使函数__qualname__
; however, it's difficult for me to use for my use case because:但是,我很难将其用于我的用例,因为:
__qualname__
returns a string. __qualname__
返回一个字符串。 For my usecase, I need references to the parent object(s).对于我的用例,我需要对父对象的引用。
__qualname__
sometimes returns the super
class instead of the referenced class. __qualname__
有时返回super
类而不是引用的类。 For example:例如:
class Parent(): def __init__(self): pass class Child(Parent): pass print(Child.__init__.__qualname__) # Prints: "Parent.__init__"
The package I am developing needs to be robust, and the edge cases for __qualname__
are not documented as far as I can tell.我正在开发的包需要健壮,据我
__qualname__
的边缘情况没有记录。
Outside of parsing the Python file with ast
, can __qualname__
be reimplemented in Python3 with inspection?除了使用
ast
解析 Python 文件之外, __qualname__
可以通过检查在 Python3 中重新实现? How does Python implement __qualname__
? Python 是如何实现
__qualname__
? In reimplementing the core functionality, I think I'll be able to adapt it for my use case.在重新实现核心功能时,我想我将能够针对我的用例进行调整。
Prior Research:先前的研究:
qualname
implementation: https://github.com/wbolster/qualname/blob/master/qualname.py qualname
实现: https : //github.com/wbolster/qualname/blob/master/qualname.py__qualname__
to get references: Get defining class of unbound method object in Python 3__qualname__
以获取引用: 在 Python 3 中获取未绑定方法对象的定义类__qualname__
stackoverflow: Reproduce effects of Python 3.3 __qualname__ in earlier Pythons __qualname__
stackoverflow: 在早期 Python 中重现 Python 3.3 __qualname__ 的效果I was unable to find the qualname implementation in the Python source code.我无法在 Python 源代码中找到 qualname 实现。
You're not going to get what you want.你不会得到你想要的。 You want
your_thing(Parent.__init__)
to say something about Parent
and your_thing(Child.__init__)
to say something about Child
, but Parent.__init__
and Child.__init__
are the same exact object.您希望
your_thing(Parent.__init__)
说一些关于Parent
而your_thing(Child.__init__)
说一些关于Child
,但Parent.__init__
和Child.__init__
是完全相同的对象。
You've accessed that object two different ways, but Python keeps no record of that.您已经通过两种不同的方式访问了该对象,但 Python 并没有对此进行记录。 Whatever you implement will only receive a function object, without the information you're looking for.
无论您实现什么,都只会收到一个函数对象,而没有您正在寻找的信息。
Even if you do some horrible stack inspection thing to look at the fact that the source code for your_thing(Child.__init__)
says "Child" in it, that won't work for cases where the function gets stored in a variable or passed around through a few more layers of function calls.即使您做了一些可怕的堆栈检查来查看
your_thing(Child.__init__)
的源代码在your_thing(Child.__init__)
说“Child”这一事实,这也不适用于函数存储在变量中或传递的情况通过更多的函数调用层。 It'll only work, unreliably, for a fraction of cases where you don't need it because you already had the information you wanted when you were writing the code.它只会在您不需要它的一小部分情况下不可靠地工作,因为您在编写代码时已经拥有所需的信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.