4

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 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.