有没有一个 python IDE 可以在您将鼠标悬停在变量上时告诉您该变量的类型?

发布于 2024-11-10 16:25:13 字数 106 浏览 1 评论 0原文

有时我写了一些项目,直到几个月后才返回它们。不幸的是,我忘记了要传递给函数的内容。我希望能够将鼠标悬停在参数上并查看整数、字符串、某些类等类型。是否有 IDE 可以为我执行此操作?任何帮助表示赞赏。

Sometimes I write projects and don't return to them until months later. Unfortunately for me I forget what was intended to be passed into a function. I would like to be able to hover over an argument and see the type such as integer, string, some class, etc. Is there an IDE out there that will do this for me? Any help is appreciated.

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

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

发布评论

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

评论(3

海螺姑娘 2024-11-17 16:25:13

无法正常推断类型,因此任何 IDE 都无法做到这一点。为什么不直接使用文档字符串呢?

def foo(a, b):
    """
    Take your arguments back, I don't want them!

    a -- int
    b -- str
    """

    return a, b

在 Python 3 中,您还可以利用函数注释:

def foo(a: int, b: str):
    """Take your arguments back, I don't want them!"""

    return a, b

There is no way to infer the type normally, so no IDE will be able to do this. Why not just use docstrings?

def foo(a, b):
    """
    Take your arguments back, I don't want them!

    a -- int
    b -- str
    """

    return a, b

In Python 3 you could also take advantage of function annotations:

def foo(a: int, b: str):
    """Take your arguments back, I don't want them!"""

    return a, b
公布 2024-11-17 16:25:13

在 Python 中,如果不实际运行代码并查看它们最终引用的内容,就不可能一般地推断变量的类型。即便如此,许多 Python 代码都适用于许多不同的类型,因此实际上不存在真正唯一的变量类型。

例如,any 内置函数会迭代其参数,如果其任何元素为 true,则返回 True。听起来像是一个从 bool 列表到 bool 的函数?错误的。它所需要的只是可以迭代的东西。这包括列表、元组、字典、集合、生成器和更多内置类型,更不用说实现迭代器协议的用户定义的类。因此,您甚至无法推断出这一点,因为对某个列表调用了 any 。并且任何Python值(我相信)都可以在布尔上下文中被评估为真/假,因此序列的内容甚至不必是布尔值。

大多数代码不使用 Python 的完整“动态性”,并且仅期望在每个变量中放入单一类型的值。因此从理论上讲,您有时可以使用一些注释和启发式方法来进行类型推断。我不知道有任何 IDE 尝试过这样做,而且我不相信它实际上是实用的。

从根本上讲,您需要有关如何调用函数/类以及它们返回什么类型的内容的文档。如果你缺乏这方面的知识,无论你是否有类型信息,你都会犯错误。

In Python, it is not possible to generically infer the types of variables without actually running the code and seeing what they end up referencing. And even then, much Python code works on many different types, so there really is no true unique type of a variable.

For example, the any builtin function iterates over its argument and returns True if any of its elements are true. Sounds like a function from list of bool to bool? Wrong. All it needs is something that can be iterated. This includes lists, tuples, dictionaries, sets, generators and more builtin types, not to mention user-defined classes that implement the iterator protocol. So there's no way you can even infer that because any is called on something that it's a list. And any python value (I believe) can be evaluated as true/false in boolean context, so the contents of the sequence don't even have to be bools.

Most code doesn't use the full "dynamism" of Python, and is only expected to put values of a single type in each variable. So theoretically, you could use some annotations and heuristics to do type inference some of the time. I am not aware of any IDE that has attempted to do this, and I don't believe it's actually practical.

Fundamentally, you need documentation on the way your functions/classes should be called and what kinds of things they return. If you lack this knowledge, you will make mistakes whether you have type information or not.

酷到爆炸 2024-11-17 16:25:13

PyCharm 中,您可以按住 CTRL 并指向标识符以查看其推断类型。 PyCharm 可以从赋值和返回值推断许多类型,还可以从文档字符串和 python3 函数注释推断参数和返回类型。

In PyCharm, you can hold CTRL and point at an identifier to see its inferred type. PyCharm can infer many types from assignments and return values, and can also infer argument and return types from both docstrings and python3 function annotations.

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