16
votes

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?

3
Writing bad Python code just to please an IDE doesn't sound like such a great idea to me... - Tim Pietzcker
once you've used a.your_attribute in your code, pyDev-eclipse will keep showing that attribute after pressing . in list with CTRL + SPACE, with its intelligence !!! - shahjapan
If you have to make your IDE absolutely useful, you will have to morph python into java .. including specifying the type explicitly. - pyfunc
I'm currently switching from C#/Java to Python and the dynamic types seem to create a lot more problems than they solve. They just feel messy and a step backwards. - Damien
Most decent Python IDEs automatically show possible methods on an object when you type obj. : Eclipse, spyder, Wing, Canopy... - smci

3 Answers

11
votes

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

x: int = 3

or:

def f(x: int):
    return x
9
votes

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.

4
votes

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