PyCharm 中未自动完成的类方法列表 [英] List of class method not autocompleting in PyCharm

查看:39
本文介绍了PyCharm 中未自动完成的类方法列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Python 新手,我有一个关于使用 Pycharm 自动完成列表中对象的方法调用的问题.

I am new to python and I have a question about autocompletion of method calls on an object within a list using Pycharm.

我有一个名为 foo() 的类:

I have a class called foo():

class foo(object):

    def __init__(self):
        self.num = 10


    def getNum(self):
        return self.num

然后我创建一个名为 myList 的列表,并向其附加一个 foo() 对象.出于某种原因,当我尝试调用该对象时,该方法没有出现.

I then create a list called myList and append a foo() object to it. For some reason when I go to try and call the object the method doesn't show up.

但是,如果我用 print(myList[0].getNum()) 完成代码,它确实会打印出 10.

However, if I complete the code with print(myList[0].getNum()), it will indeed print out 10.

另外,如果我简单地创建一个变量 x 并将它分配给一个 foo() 对象,它会像这样显示得很好:

Also, if I simply create a variable x and assign it to a foo() object, it will show up just fine like so:

我尝试创建一个 y 变量并将其分配给 myList[0] 以查看是否可以显示该方法,但仍然没有运气.

I tried to create a y variable and assign it to myList[0] to see if I can get the method to show up, but still no luck.

这只是一个 IDE 问题,还是在对列表中的对象进行方法调用时我遗漏了更大的图景.

Is this just an IDE issue or is there a bigger picture that I am missing when doing method calls working with objects inside of list.

推荐答案

原因是python有动态类型.没有限制 myList 必须只包含 foo 对象.因此 PyCharm 无法知道 myList[0] 是一个 foo 来为您提供 foo 的自动完成功能(仅在运行时知道).

The reason is that python has dynamic typing. There is no constraint that myList must contain ONLY foo objects. So PyCharm can't know that myList[0] is a foo to give you the autocomplete for foo (that's only known at runtime).

以这个例子为例:

class foo(object):

    def __init__(self):
        self.num = 10


    def getNum(self):
        return self.num

class bar(object):

    def __init__(self):
        self.num = 10


    def getNum2(self):
        return self.num

myList = []
myList.append(foo())
myList.append(bar())
print(myList[0])

PyCharm 不知道是否为 foo() 或 bar() 提供自动完成功能,因此它也不会给您.

PyCharm doesn't know whether to give you the autocomplete for foo() or bar() so it doesn't give you either.

关于 x 的最后一种情况有效,因为您明确将 x 指定为 foo 对象,因此 Pycharm 知道并为您提供 foo 的自动完成功能.

The last case regarding x works because you explicitly assigned x as a foo object, so Pycharm knows and gives you the autocomplete for foo.

这篇关于PyCharm 中未自动完成的类方法列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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