PyCharm 能否以正确的顺序显示变量的字段? [英] Can PyCharm display the fields of a variable in correct order?

查看:79
本文介绍了PyCharm 能否以正确的顺序显示变量的字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我定义了一个包含几个字段的数据类.当我打印它时,它们以正确的顺序显示:

I defined a data class which has a few fields. When I print it, they are displayed in the correct order:

def test():
    @dataclass
    class Image:
        width: int
        height: int
        pixels: object

    image = Image(width=4, height=3, pixels=list(range(12)))
    print(image)
    pass

这段代码的输出很好:

test.<locals>.Image(width=4, height=3, pixels=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])

但是如果我检查 PyCharm 中的变量(通过在最后一行的断点处停止,表示 pass),它会按字母顺序显示字段,这令人困惑:

But if I examine the variable in PyCharm (by stopping at a breakpoint at the last line, which says pass), it shows me the fields in alphabetical order, which is confusing:

我可以解决这个问题吗?

Can I fix this behavior?

我使用的是 PyCharm 2019.1.2.

I am using PyCharm 2019.1.2.

推荐答案

我研究了在 window.html" rel="nofollow noreferrer">调试工具窗口".供我自己使用并尝试解决另一个问题 在子类化 str 时在 Pycharm 调试器中显示类属性,我认为通过显示完美地说明了这一点类在变量窗格"中的显示方式完全不同;只是因为它继承自字符串.

I looked into the possibilities of adjusting how objects are displayed in the "Variables pane" of the "Debug tool window". For my own use and trying to solve another question Showing class attributes in the Pycharm debugger when subclassing str which I think exemplifies this perfectly by showing that a class is displayed completely differently in the "Variables pane" just because it inherits from string.

我可以解决这个问题吗?

Can I fix this behavior?

简单的答案是:(目前)无法完成.PyCharm 调试器会自动决定如何在变量窗格"中显示对象.无法使用设置从 IDE 内部进行配置.

Simple answer is: It can't be done (currently). The PyCharm debugger takes some automatic decisions in how to display the objects in the "Variable pane" that can't be configured from inside the IDE using settings.

此问题中按声明顺序而不是字母顺序显示属性的具体情况.这个问题类似于File > Settings > Build, Execution, Deployment > Debugger > 数据视图 > 按字母顺序对值进行排序,但这是针对变量内的值顺序(例如,它按字母顺序对未排序的 set 的元素进行排序以进行显示) - 而不是范围内的属性顺序.

The specific case in this question of displaying the attributes in declaration order instead of alphabetic order. This issue is similar to the option at File > Settings > Build, Execution, Deployment > Debugger > Data Views > Sort values alphabetically but that is for value order inside a variable (for example it sorts the elements of an unsorted set alphabetically for display) - not attribute order in a scope.

这可能是我发现的 PyCharm 调试器的唯一限制.有一些不错的替代方案,但没有变量窗格"那么方便:

This is perhaps the only limitation of the PyCharm debugger I've found. There are good alternatives that nevertheless don't have the same convenience as the "Variables pane":

  1. 使用手表面板"- 允许完全控制您看到的内容和顺序.它的缺点是必须设置手表.
  2. 使用 Evaluate Expression 有必须被调用的缺点.
  3. 使用控制台标签.
  1. Use the "Watches pane" - allows full control over what you see and in what order. It has the drawback of having to set the watches.
  2. Use Evaluate Expression Has the drawback of having to be invoked.
  3. Use Console tab.

<小时>

最后,也许可以"自定义变量窗格"的显示.但我认为这将永远是一个黑客而不是一个正常的用例.如果我们注意到调试器是如何被调用的,我们可以检查 pydevd.py 已实现,并且它接受命令行参数.也许有一个未记录的 CLI 参数来配置变量窗格".显示.


Finally, "maybe it is possible" to customize the display of the "Variables pane" but I think this would always be a hack and not a normal use case. If we notice how the debugger is invoked we can examine how pydevd.py is implemented and the command line arguments it accepts. Maybe there is an undocumented CLI argument to configure the "Variables pane" display.

然而,只要看一眼 pydevd.py 模块的复杂性就足以简单地使用手表面板"如果需要,作为替代方案.

However, one glance at the complexity of the pydevd.py module is enough to simply use the "Watches pane" as an alternative if need be.

C:\path_to_interpreter\python.exe

"C:\Program Files\JetBrains\PyCharm 2019.3.2\plugins\python\helpers\pydev\pydevd.py"

--multiproc --qt-support=auto --client 127.0.0.1 --port 51753 --file 

C:/path_to_your_module.py

Connected to pydev debugger (build 211.7142.13)

这篇关于PyCharm 能否以正确的顺序显示变量的字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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