本文介绍了Pycharm:如何为外部对象类型设置自定义字符串函数(即Type Renderer)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将PyCharm配置为使用自定义函数在调试会话中显示类型的__str__表示形式?我指的是内置类型或从第三方库导入的类型,我不希望对其进行修改.

Is it possible to configure PyCharm to use a custom function to display the __str__ representation of a type in a debug session? I am referring to built-in types or types imported from third party libraries which I would rather not modify.

例如,我希望调试器中的字符串不像{lxml.html.HtmlElement} <Element tr at 0x10e2c1418>那样,而是要输出etree.tostring(element).

For example, instead of a string in the debugger like {lxml.html.HtmlElement} <Element tr at 0x10e2c1418> I would like to have the output of etree.tostring(element).

Intellij Idea具有 Java类型渲染器 ,您可以在其中为任何类型设置自定义toString()方法,以便在调试期间这些类型将使用自定义toString()渲染器.在PyCharm中是否可以使用或实现类似的功能?

Intellij Idea has Java Type Renderers where you can set a custom toString() method for any type, so that during debugging those types will use your custom toString() renderers. Is similar functionality available or achievable in PyCharm?

我尝试过这种方法:

# for lxml.html

lxml.html.HtmlElement.__str__ = lxml.html.etree.tostring

这给出了lxml.html的预期结果,但是感觉像一个丑陋的解决方法,我想找到一种不需要外部库.

This gives the expected result for lxml.html, but it feels like an ugly workaround, and I would like to find a way to do it that does not require monkey-patching external libraries.

这种方法的缺点是它不适用于lxml.etree.Element,因为设置lxml.etree.Element.__str__ = lxml.etree.tostring无效,因为它委托给lxml.etree._Element,这是具有只读__str__的C本机模块

The downside of this approach is that it doesn't work for example with lxml.etree.Element because setting lxml.etree.Element.__str__ = lxml.etree.tostring has no effect, as it delegates to lxml.etree._Element which is C native module with a read-only __str__.

推荐答案

Python没有此类功能,请在PyCharms的错误跟踪器中为相应的票证投票: PY-21984 .

There is no such feature for Python, please vote for the corresponding ticket in PyCharms' bug tracker: PY-21984.

这篇关于Pycharm:如何为外部对象类型设置自定义字符串函数(即Type Renderer)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 21:07