简体   繁体   English

Python将函数参数视为自动补全类

[英]Python seeing function parameters as class for autocompletion

Is it possible to declare python functions with specific class parameters? 是否可以使用特定的类参数声明python函数? The reason is that I want to avoid type errors (Happens to me quiet often) 原因是我想避免类型错误(经常让我安静)

For example: 例如:

def connect(socket as socket, HOST, PORT):
    return socket.connect((HOST, PORT))

The goal here would be that if I type 'socket.' 目的是如果我输入“ socket”。 the autocompletion would show me any functions available on the socket class. 自动补全功能会告诉我套接字类上可用的任何功能。 Since I am passing it as an argument the autocompletion which pops up is empty. 由于我将其作为参数传递,因此弹出的自动补全为空。 The reason is that you can pass every object to the function. 原因是您可以将每个对象传递给函数。

Python has syntactic support for type annotations, but Python itself doesn't have autocomplete. Python对类型注释提供了语法支持,但是Python本身没有自动完成功能。 That's provided by third-party tools, like PyCharm and Jedi. 这是由第三方工具(例如PyCharm和Jedi)提供的。 Some of these applications (like PyCharm) can use static inference to get better completions if you annotate your functions properly. 如果正确注释功能,则其中一些应用程序(例如PyCharm)可以使用静态推断来获得更好的完成效果。

Say you start typing in a function into PyCharm like this, 假设您开始像这样在PyCharm中输入函数,

def foo(a_dict: dict) -> int:
    print(a_dict.i▏

Then it will be able to infer that a_dict is a dict and pop up a completion window with dict methods, like items() . 然后,可以推断a_dict是一个dict并使用dict方法(例如items()弹出一个完成窗口。

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

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