像 PDT 一样使用 PyDev 来完成代码?

发布于 2024-08-17 17:08:03 字数 258 浏览 7 评论 0原文

有没有办法通过告诉 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

江湖彼岸 2024-08-24 17:08:03

断言技巧似乎不适用于 PyDev 2.2.2 ;它仍然应该吗?

然而,我尝试过的另一个技巧如下:

class Foo(object):
    def __init__(self, bar):
       self.bar = bar
       # Tricking PyDev
       if (not self.bar):
          self.bar = Bar()
          raise Exception("Bar should not be null")

在所有情况下,它看起来都很老套,我希望有一种更干净的方式来做事(文档,注释,等等)

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 :

class Foo(object):
    def __init__(self, bar):
       self.bar = bar
       # Tricking PyDev
       if (not self.bar):
          self.bar = Bar()
          raise Exception("Bar should not be null")

In all cases, it looks pretty hacky, and I would love a cleaner way to do things (documentation, annotation, whatever)

べ繥欢鉨o。 2024-08-24 17:08:03

实际上,如果您执行断言 isinstance() 即可

,例如:

a = function()
assert isinstance(a, MyClass)
a. <- would get the proper completions

请注意,Pydev 确实分析了函数的返回,因此,它可能在许多情况下都知道这一点。

另外,如果您使用 python -O (这将删除断言),则不应该有运行时惩罚

Actually, you can if you do an assert isinstance()

E.g.:

a = function()
assert isinstance(a, MyClass)
a. <- would get the proper completions

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)

浮光之海 2024-08-24 17:08:03

不(请参阅 文档< /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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文