像 PDT 一样使用 PyDev 来完成代码?
有没有办法通过告诉 PyDev 变量的类型来帮助它完成代码?
使用 PDT,您可以使用类似 PHPDoc 的语法来达到此目的:
/* @var $my_var MyClass */
$my_var = myFunction();
// PDT is able to figure out that $my_var is a MyClass object.
但到目前为止,我不知道如何在 python 中执行相同的操作。
Is there a way to help PyDev code completion by telling it the type of a variable?
With PDT, you can use PHPDoc-like syntax for such purpose:
/* @var $my_var MyClass */
$my_var = myFunction();
// PDT is able to figure out that $my_var is a MyClass object.
But till now, I cannot figure out how to do the same in python.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
断言技巧似乎不适用于 PyDev 2.2.2 ;它仍然应该吗?
然而,我尝试过的另一个技巧如下:
在所有情况下,它看起来都很老套,我希望有一种更干净的方式来做事(文档,注释,等等)
The assert trick does not seem to work for me with PyDev 2.2.2 ; it is still supposed to ?
However another trick I tried and that work is the following :
In all cases, it looks pretty hacky, and I would love a cleaner way to do things (documentation, annotation, whatever)
实际上,如果您执行断言 isinstance() 即可
,例如:
请注意,Pydev 确实分析了函数的返回,因此,它可能在许多情况下都知道这一点。
另外,如果您使用 python -O (这将删除断言),则不应该有运行时惩罚
Actually, you can if you do an assert isinstance()
E.g.:
Note that Pydev does analyze the return of the functions, so, it's possible that it knows that on a number of cases.
Also, that shouldn't have runtime penalties if you use python -O (which will remove the asserts)
不(请参阅 文档< /a>)。看起来 PyDev 可以完成导入的内容和语言关键字。
不过,这似乎不会出现很多。有问题的变量似乎只有在作为没有默认值的函数参数传入时才会被 pydev 识别。并且,如果您有一个在您自己的类上运行的函数,那么它似乎应该是一个类成员(所以自动完成功能已经可以工作了)。
Nope (see the docs). It looks like PyDev does completion of imported stuff and language keywords.
It doesn't seem like this would come up a lot though. The variable in question seems like it would only be unknown to pydev if it were passed in as a function argument with no default value.And, if you have a function operating on your own class, it seems like that should be a class member (so autocomplete would already work).