子类化 str 时在 PyCharm 调试器中显示类属性 [英] Showing class attributes in the PyCharm debugger when subclassing str

查看:26
本文介绍了子类化 str 时在 PyCharm 调试器中显示类属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在调试器中查看 str 子类的类属性.

I want to see class attributes for a subclass of str in the debugger.

参考图片 - 我想在 a (这是一个 POSString 对象)旁边有下拉箭头.换句话说,在 Pycharm 调试器中,我希望可以选择双击 a 并查看属性 bar 的值为 HELLO",与 foo 对象完全一样.

Refer to the picture - I want to have the dropdown arrow next to a (which is a POSString object). In other words, in the Pycharm debugger I want to have the option to doubleclick a and see the attribute bar with value "HELLO", exactly like for the foo object.

这在 PyCharm 中是否可行,如果是,如何实现?

Is this possible in PyCharm, and if yes how?

两个类:

class Foo:
    bar = "HELLO"


class POSString(str):
    bar = "HELLO"

    def __init__(self, pos="", *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.pos = pos

    def __hash__(self):
        return hash((str(self), self.pos))

    def __eq__(self, other):
        return super().__eq__(other) and self.pos == other.pos

以及我如何创建我的两个对象:

And how I create my two objects:

foo = Foo()
a = POSString("banana")

我使用的是 Python 3.7.1.

I am using Python 3.7.1.

PyCharm:

PyCharm 2018.2.5 (Professional Edition)
Build #PY-182.5107.22, built on November 13, 2018
JRE: 1.8.0_152-release-1248-b22 amd64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Linux 4.15.0-43-generic

推荐答案

PyCharm 目前没有在 Variables Tab 带有该细节的调试工具窗口.IDE 会根据对象的类型自动做出大多数显示决策.当前唯一可用的选项是使用 File > Settings > 中的设置对单个变量内的值进行排序构建、执行、部署 > Debugger > 数据视图 > 按字母顺序排列值.(有关更详细的说明,请参阅这篇博文).

PyCharm doesn't currently have a setting to configure display in the Variables Tab of the Debug tool window with that detail. The IDE takes most display decision automatically based on the type of the object. The only option currently available is sorting values inside a single variable using the setting at File > Settings > Build, Execution, Deployment > Debugger > Data Views > Sort values alphabetically. (See this post for a more detailed explanation).

这对于调试来说可能不方便,问题中的示例class显示的完全像一个字符串,因为该类继承自字符串.这是 IDE 的决定,目前无法更改它(除了使用 JetBrains 发布功能请求).

This can be inconvenient for purpose of debugging, the example class in the question is displayed exactly like a string because the class inherits from string. That's an IDE decision and there's currently no way to change it (apart from posting a feature request with JetBrains).

替代方案是设置手表"在变量选项卡中或使用控制台.另一个更不方便的替代方法是将类本身更改为不从字符串继承(在这种情况下,它将像常规类一样显示)但是这可能会破坏代码中的任意数量的功能,因此获得的调试便利可能会被造成的不便.

The alternatives are setting "Watches" in the Variables Tab or using the Console. The other more inconvenient alternative is changing the class itself to not inherit from string (in which case it will be displayed like regular classes are) however this can break any number of functionalities in your code, so the debugging convenience gained can be outweighed by the inconveniences caused.

这篇关于子类化 str 时在 PyCharm 调试器中显示类属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆