首页 > 解决方案 > 如何在类型化属性的类定义中使 pycharm 的自动完成工作?

问题描述

当我在 init 语句中设置对象的类型时,自动完成在 init 语句中起作用:

init 函数中的类型定义和自动完成

from PyQt5.QtWidgets import QLabel

class TestAutoComplete:

    def __init__(self):
        self.testLabel: QLabel
        self.testLabel.

然后,当我想在另一个函数中自动完成相同的变量时,它不起作用:

自动完成在其他功能中不起作用

from PyQt5.QtWidgets import QLabel

class TestAutoComplete:

    def __init__(self):
        self.testLabel: QLabel

    def otherFunction(self):
        self.testLabel.

如何为属性设置一次类型,然后在每个类函数定义中自动完成?

亲切的问候巴斯蒂安

标签: pythonobjectpycharmtyping

解决方案


似乎我偶然找到了答案。您必须键入一个类变量:

使用类变量上的类型的函数中的自动完成

from PyQt5.QtWidgets import QLabel

class TestAutoComplete:
    testLabel: QLabel

    def __init__(self):
        pass

    def otherFunction(self):
        self.testLabel.

这对我来说似乎很奇怪。我是一个 python 初学者,但我想已经明白这个屏幕截图中的 testLabel 是一个类变量,只能通过 TestAutoComplete.testLabel 而不是通过 self.testLabel 访问。这可能是 pycharm 自动完成中的错误吗?


推荐阅读