简体   繁体   English

Pycharm Intellisense用于自定义类的令人惊讶的行为

[英]Pycharm Intellisense for custom class surprising behavior

Recently, I was using pycharm to implement some simple algorithm. 最近,我正在使用pycharm实现一些简单的算法。 But I found a rather surprising fact. 但是我发现了一个相当令人惊讶的事实。

Here is my code 这是我的代码

def main():
n = Node(2)
n.nextNode=Node(1)

def TraverseNodes(node):
    if node.nextNode = None
        print(node.num)
    else:
        print(node.num)
        TraverseNodes(node.nextNode)

class Node():
    nextNode = None
    num = None
    def __init__(self,num):
        self.num = num
    def appendNext(self,next):
        self.nextNode=next

main()

As far as I know, python is a dynamic type language, which means the type is interpreted at runtime. 据我所知,python是一种动态类型语言,这意味着类型是在运行时解释的。

So pycharm should't know what type does the "node" parameter in TrasverseNode method belongs to. 因此,pycharm不应该知道TrasverseNode方法中的“ node”参数属于哪种类型。

But when I actually wrote the details of the method, the intellisense seemed to be able to infer the type is node ?! 但是当我实际写出方法的细节时,intellisense似乎能够推断出类型是node?!。 在此处输入图片说明

( I tried different parameter name , only "node" could lead to this behavior , others can't. Does this means pycharm can infer the type by the similarity in names ? ) (我尝试使用不同的参数名称,只有“ node”可能导致此行为,其他参数则无法。这是否意味着pycharm可以通过名称的相似性来推断类型?)

Thank you very much. 非常感谢你。

Yes. 是。 If you have a variable and a class with the same name as the variable but capitalized, PyCharm does assume that the variable may be an instance of that class, and suggests the members of this class in the completion popup. 如果您有一个变量和一个与变量名称相同但大写的类,则PyCharm会假定该变量可能是该类的实例,并在完成弹出窗口中建议该类的成员。

(This only affects completion - for example, it will not show warnings if you access a member which is not defined in this class.) (这只会影响完成-例如,如果您访问未在此类中定义的成员,它将不会显示警告。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM