Explicitly declaring a variable type in Python

20,404

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.

Share:
20,404
lalli
Author by

lalli

The Average Genius.

Updated on October 20, 2020

Comments

  • lalli
    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 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?

  • aaronasterling
    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
    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
    NameError about 2 years
    Will a warning or Exception be raised if I assign a string to it after explicitly declaring as an int?