Pycharm: how to set a custom string function (ie Type Renderer) for external object types?

爱⌒轻易说出口 提交于 2019-12-10 10:25:39

问题


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.

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 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?

I tried this approach:

# for lxml.html

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

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.

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__.


回答1:


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



来源:https://stackoverflow.com/questions/50891018/pycharm-how-to-set-a-custom-string-function-ie-type-renderer-for-external-obj

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!