相关疑难解决方法(0)

我如何告诉PyCharm参数的类型是什么?

在构造函数,赋值和方法调用方面,PyCharm IDE非常擅长分析我的源代码并确定每个变量应该是什么类型.我喜欢它,因为它给了我很好的代码完成和参数信息,如果我尝试访问不存在的属性,它会给我警告.

但是当谈到参数时,它什么都不知道.代码完成下拉列表无法显示任何内容,因为它们不知道参数的类型.代码分析无法查找警告.

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

peasant = Person("Dennis", 37)
# PyCharm knows that the "peasant" variable is of type Person
peasant.dig_filth()   # shows warning -- Person doesn't have a dig_filth method

class King:
    def repress(self, peasant):
        # PyCharm has no idea what type the "peasant" parameter should be
        peasant.knock_over()   # no warning even though knock_over doesn't exist

King().repress(peasant)
# Even if I call the method once with a Person instance, …
Run Code Online (Sandbox Code Playgroud)

python type-hinting code-completion pycharm

169
推荐指数
3
解决办法
4万
查看次数

标签 统计

code-completion ×1

pycharm ×1

python ×1

type-hinting ×1