Explicitly declaring a variable type in Python
Solution 1
in case you want methods callable on a type ...you can always use dir(var) in python console...
Solution 2
As of Python 3, you can explicitly declare variables by type:
x: int = 3
or:
def f(x: int):
return x
Solution 3
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.
Comments
-
lalli over 3 years
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 usefile.
and choose the function and write its arguments and then replacefile
with the variable name.Is there a way to declare a variable type explicitly in Python, so that my IDE can be more useful?
-
aaronasterling over 13 years+1 I think that this is the right answer. Learn the methods available on it the hard way and quit your bellyaching.
-
GrowingCode247 almost 4 years@aaronasterling How silly. "Work harder than you have to simply because languages like Python and Javascript make things more difficult than they have to be."
-
NameError about 2 yearsWill a warning or Exception be raised if I assign a string to it after explicitly declaring as an
int
?