Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

python - Can PyCharm display the fields of a variable in correct order?

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

The output of this code is good:

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

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:

PyCharm watch

Can I fix this behavior?

I am using PyCharm 2019.1.2.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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?

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.

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.

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

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.

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_interpreterpython.exe

"C:Program FilesJetBrainsPyCharm 2019.3.2pluginspythonhelperspydevpydevd.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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...