简体   繁体   English

在Python中明确声明变量类型

[英]Explicitly declaring a variable type in Python

I use pyscripter for coding, it supports auto-completion. 我使用pyscripter进行编码,它支持自动全。 So, when I say: 所以,当我说:

a = []
a.

It gives me all the list functions. 它给了我所有列表功能。 similarly with strings I do b='' . 与字符串类似,我做b=''

But for the file type, I have to use file. 但是对于file类型,我必须使用file. and choose the function and write its arguments and then replace file with the variable name. 并选择函数并编写其参数,然后将file替换为变量名。

Is there a way to declare a variable type explicitly in Python, so that my IDE can be more useful? 有没有一种方法可以在Python中显式声明变量类型,以便我的IDE更加有用?

Python has no type declarations. Python没有类型声明。 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. Python 3引入了一种称为函数注释的东西,Guido有时将其称为“不是类型声明的东西”,因为它最明显的用途是提供类型信息作为提示。

As others have mentioned, various IDEs do a better or worse job at auto-completing. 正如其他人提到的那样,各种IDE在自动完成方面做的更好或更差。

万一你想在一个类型上调用方法...你总是可以在python控制台中使用dir(var)...

As of Python 3, you can explicitly declare variables by type: 从Python 3开始,您可以按类型显式声明变量:

x: int = 3

or: 要么:

def f(x: int):
    return x

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM