在 Python 中显式声明变量类型

发布于 2024-09-29 16:53:19 字数 329 浏览 1 评论 0原文

我使用 pyscripter 进行编码,它支持自动完成。所以,当我说:

a = []
a.

它给了我所有的列表功能。与字符串类似,我的做法是 b=''

但对于 file 类型,我必须使用 file. 并选择函数并写入其参数,然后将 file 替换为变量名称。

有没有办法在 Python 中显式声明变量类型,以便我的 IDE 更加有用?

I use pyscripter for coding, it supports auto-completion. So, when I say:

a = []
a.

It gives me all the list functions. similarly with strings I do b=''.

But for the file type, I have to use file. and choose the function and write its arguments and then replace file with the variable name.

Is there a way to declare a variable type explicitly in Python, so that my IDE can be more useful?

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

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

发布评论

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

评论(3

陌生 2024-10-06 16:53:22

如果您想要在类型上调用方法...您始终可以在 python 控制台中使用 dir(var)...

in case you want methods callable on a type ...you can always use dir(var) in python console...

妄想挽回 2024-10-06 16:53:21

Python 没有类型声明。 Python 3 引入了名为函数注释的东西,Guido 有时将其称为“不是类型声明的东西”,因为它最明显的用途是提供类型信息作为提示。

正如其他人提到的,各种 IDE 在自动完成方面做得更好或更差。

Python has no type declarations. Python 3 introduces something called function annotations, which Guido sometimes refers to as "the thing that isn't type declarations," because the most obvious use of it will be to provide type information as a hint.

As others have mentioned, various IDEs do a better or worse job at auto-completing.

阳光①夏 2024-10-06 16:53:19

从 Python 3 开始,您可以通过类型显式声明变量:

x: int = 3

或:

def f(x: int):
    return x

As of Python 3, you can explicitly declare variables by type:

x: int = 3

or:

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