Pydev 代码完成一切
在许多情况下(例如函数参数)Pydev 并不静态地知道变量的类型。因此,代码完成(在 .
之后或使用 ctrl+space
时)不起作用。
在大多数情况下,您在设计软件时知道运行时的类型。有没有办法提示 Pydev 正确编码完成它?
我想这可能需要特定的 Pydev 功能,甚至可能需要新的 Python PIP。
这实际上似乎是所有动态语言的普遍问题......
更新:
也许有一个例子可以澄清:
def some_func(a_list, an_object):
a_list.app # Here I would not get code completion for append
如果 Pydev(或 PIP)支持的话,可以工作的例子:
from someobj import SomeObject
def some_func(a_list, an_object):
# typecast: a_list=list
# typecast: an_object=SomeObject
a_list.app # Now code completion would show append
我不认可这个特定的方法 - 它只是一个例子可以工作的系统。同样,当然这不应该是强制性的 - 但有时缺乏暗示类型的可能性是令人烦恼的。
In many cases (such as function parameters) Pydev doesn't statically know the type of a variable. Therefore code completion (after .
or when using ctrl+space
) doesn't work.
In most cases, you know what type will be in run-time as you are designing the software. Is there a way to hint Pydev to code completing it correctly?
I guess this may require a specific Pydev feature, or perhaps even a new Python PIP.
This is actually seems to be a generic problem with all dynamic languages...
UPDATE:
Perhaps an example is in place for clarification:
def some_func(a_list, an_object):
a_list.app # Here I would not get code completion for append
An example of something that could work, if Pydev (or a PIP) would support it:
from someobj import SomeObject
def some_func(a_list, an_object):
# typecast: a_list=list
# typecast: an_object=SomeObject
a_list.app # Now code completion would show append
I'm not endorsing this specific method - it's just an example of a system that could work. Again, of course this should not be mandatory - but sometimes the lack of the possibility to hint the type is annoying.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
[编辑]
从 PyDev 2.8.0 开始,它可以使用文档字符串和注释来发现对象的类型。
有关支持格式的详细信息,请参阅:http://pydev.org/manual_adv_type_hints.html。
[PyDev 2.8.0 之前]
以前,它仅支持断言 isinstance 调用(这仍然有效):
PyDev 将能够识别它并正确地为其提供代码完成(请注意,您可以运行稍后如果您发现没有断言的 Python 会使代码变慢: 什么Python 优化(-O 或 PYTHONOPTIMIZE)有作用吗?)
[Edit]
Since PyDev 2.8.0, it can use docstrings and comments to discover the type of objects.
See: http://pydev.org/manual_adv_type_hints.html for details on the supported formats.
[Before PyDev 2.8.0]
Previously, it only supported assert isinstance calls (and this still works):
PyDev will be able to recognize it and properly provide code-completion for it (note that you can run Python without the assertions later on if you find it's making your code slower: What does Python optimization (-O or PYTHONOPTIMIZE) do? )
从 PyDev 2.8.0 开始,可以使用 Sphinx 或 Epydoc 注释来完成代码:
http://pydev.org/manual_adv_type_hints.html
As of PyDev 2.8.0 can use Sphinx or Epydoc comments for code completion:
http://pydev.org/manual_adv_type_hints.html
如果您使用 PyCharm,您可以选择 epydoc 或 sphinx 文档字符串样式并指定参数类型和函数返回根据该样式的值(进一步讨论)
If you use PyCharm, you can pick either epydoc or sphinx docstring style and specify types of parameters and function return values according to that style (further discussion)
我通常会做什么来规避这个问题。
只需记住在测试或提交时删除变量初始化即可。
哦,顺便说一句,标记为答案的回复似乎对我不起作用。
What I usually do to circumvent this.
Just remember to delete the variable initialization when testing or committing.
Oh and btw, the response marked as an answer doesn't seem to work for me.
如果您在代码中使用 Python >= 3.5,则可以在函数声明中指定参数类型 (26.1. 类型 - 支持类型提示),因此您的代码可以是:
这种类型的语法对执行没有影响,并且不会对传递给函数的实际类型进行任何检查,但使您的代码更具可读性并使代码完成工作有效。
If you are using Python >= 3.5 in your code you can specify parameters type in function declarations (26.1. typing — Support for type hints), so you code could be:
This type of syntax, has no effects on execution and does not make any check on the real type passed o the function, but make your code more readable and makes the code completion work.