使用 pydev 时,我可以用类型名注释参数以提供上下文信息吗?

发布于 2024-12-29 07:16:11 字数 492 浏览 2 评论 0原文

我最近开始使用 pydev。看起来很棒。但有一个烦恼来自于 Python 是一种动态语言这一事实​​。当 pydev 知道类型时,许多自动完成功能都会起作用,所以

  f = open("foo.txt")

效果很好,pydev 可以找出 f 是一个文件,并给我很好的建议。

但是,当处理我自己的函数中的参数时,pydev 显然无法确定类型信息:

  def bar(x,y): #Pydev obv. cant tell exactly what x and y are

所以当我执行 x. 时,我显然没有得到任何建议。

如果能提供某种注释,让 pydev 可以选择来添加建议,并通过警告我应该这样做来帮助我编码更安全,那就太好了。我知道我的思考方式就像来自静态语言的人一样,但很多时候,参数的类型应该始终是一件事,而且只有一件事。我可以注释我的代码来帮助 pydev 吗?

I've recently started using pydev. It seems great. One annoyance though comes from the fact that python is a dynamic language. Many of the autocompletion features will work when pydev knows the type, so

  f = open("foo.txt")

works great, pydev can figure out that f is a file and gives me great suggestions.

However, when dealing with parameters in my own functions, pydev obviously can't determine type information:

  def bar(x,y): #Pydev obv. cant tell exactly what x and y are

So I obviously don't get any suggestions when I do x..

It would be great to provide some kind of annotation that pydev can pick up to add suggestions and also to help me code a little safer by warning me I should. I know I'm thinking like someone coming from a static language, but much of the time the type of an argument should always be one thing and only one thing. Can I annotate my code to help pydev out?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

可是我不能没有你 2025-01-05 07:16:11

如果 x 是一个列表,那么这应该有效:

def bar(x,y):
   assert isinstance(x, list)

If x is a list, then this should work:

def bar(x,y):
   assert isinstance(x, list)
断肠人 2025-01-05 07:16:11

您可以使用:

def bar(x, y):
    """This function does something.

        @type x: str
            Describe param x
        @type y: int
            Or don't describe. But not in same line.

        @rtype int
            Function returns int
     """
     pass

You may use:

def bar(x, y):
    """This function does something.

        @type x: str
            Describe param x
        @type y: int
            Or don't describe. But not in same line.

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