如何检查 Python 的类层次结构?

发布于 2024-12-17 06:06:29 字数 253 浏览 3 评论 0原文

假设我有一个 X 类,我如何检查哪个是基类,以及它们的基类等?

我将 Eclipse 与 PyDev 结合使用,例如,对于 Java,您可以在类名称上键入 CTRL + T 并查看层次结构,例如:

java.lang.Object
   java.lang.Number
       java.lang.Integer

Python 可以吗?

如果在 Eclipse PyDev 中不可能,我在哪里可以找到此信息?

Assuming I have a class X, how do I check which is the base class/classes, and their base class/classes etc?

I'm using Eclipse with PyDev, and for Java for example you could type CTRL + T on a class' name and see the hierarchy, like:

java.lang.Object
   java.lang.Number
       java.lang.Integer

Is it possible for Python?

If not possible in Eclipse PyDev, where can I find this information?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

缘字诀 2024-12-24 06:06:29

尝试 inspect.getclasstree()

将给定的类列表排列成嵌套列表的层次结构。在出现嵌套列表的地方,它包含从其条目紧接在列表之前的类派生的类。每个条目都是一个二元组,包含一个类及其基类的元组。如果 unique 参数为 true,则给定列表中每个类的返回结构中将出现一个条目。否则,使用多重继承的类及其后代将多次出现。

Try inspect.getclasstree().

Arrange the given list of classes into a hierarchy of nested lists. Where a nested list appears, it contains classes derived from the class whose entry immediately precedes the list. Each entry is a 2-tuple containing a class and a tuple of its base classes. If the unique argument is true, exactly one entry appears in the returned structure for each class in the given list. Otherwise, classes using multiple inheritance and their descendants will appear multiple times.

云淡风轻 2024-12-24 06:06:29

按 f4 键并突出显示类名以打开层次结构视图。

Hit f4 with class name highlighted to open hierarchy view.

神经暖 2024-12-24 06:06:29

此外,每个类都带有一个名为 __mro__它给出了给定类可以继承方法或属性的所有父类。从左到右阅读它们。例如:

assert bool.__mro__ == (<class 'bool'>, <class 'int'>, <class 'object'>)
assert True.__class__.__mro__ == (<class 'bool'>, <class 'int'>, <class 'object'>)

Also, every class carries around with it an attribute called __mro__ which gives all the parent classes from which a given class could inherit methods or attributes. Read them from left to right. For example:

assert bool.__mro__ == (<class 'bool'>, <class 'int'>, <class 'object'>)
assert True.__class__.__mro__ == (<class 'bool'>, <class 'int'>, <class 'object'>)
小忆控 2024-12-24 06:06:29

您还可以将类层次结构可视化为图片。
我使用 Graphviz DOTpyreverse 可视化和探索大量混乱的类(示例)。

假设您想从 parso 库:

# Generate classes_tree_full.png with full UML-style class boxes
$ pyreverse -mn    -o png -p tree_full  Lib\site-packages\parso\python\tree.py

# Generate classes_tree_short.png with only class names in boxes
$ pyreverse -mn -k -o png -p tree_short Lib\site-packages\parso\python\tree.py

如果您想查看限定的类名,例如,请将 -mn 更改为 -myparso.python.tree.Literal 而不仅仅是 Literal
注意:pyreverse还通过glob<支持多个文件/code>模式。

You can also visualize class hierarchy as a picture.
I use Graphviz DOT and pyreverse to visualize and explore a large mess of classes (an example).

Assuming you want to draw all classes from tree.py in parso library:

# Generate classes_tree_full.png with full UML-style class boxes
$ pyreverse -mn    -o png -p tree_full  Lib\site-packages\parso\python\tree.py

# Generate classes_tree_short.png with only class names in boxes
$ pyreverse -mn -k -o png -p tree_short Lib\site-packages\parso\python\tree.py

Change -mn to -my if you want to see qualified class names, ex. parso.python.tree.Literal instead of just Literal.
Note: pyreverse also supports multiple files via globpatterns.

我爱人 2024-12-24 06:06:29

点击 command+o,然后按 Ctrl+O 显示父层次结构

签出此博客 http://pydev.blogspot.jp/2015/03/navigating-through-your-code-when-in.html

Hit command+o, then Press Ctrl+O to show parent hierarchy

checkout this blog http://pydev.blogspot.jp/2015/03/navigating-through-your-code-when-in.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文