Python Strongly type lists
Asked Answered
D

4

6

I am using eclipse for python and I am facing a problem. I have many classes with many properties and want a list of objects from one of my declared classes. The problem is: When I am accessing any item from the list, the IDE does not know its type because in python we do not declare the variable with type, so there is no auto complete and I have to go to the class to copy the attribute name.

To make idea more clear:

class AutomataBranch(object):
    def __init__(selfparams):
        self.Name="";
        self.nodes=[];

class LanguageAutomata(object):    
    def __init__(selfparams):
        self.cfgAutomata=[];#This has AutomaBranch Type

Now in any method in LanguageAutomata class if I wrote: cfgAutomata. Then it wont give me the Name attribute Is there any solution for that?

Derickderide answered 17/8, 2009 at 14:53 Comment(4)
Are you using PyDev? pydev.sourceforge.netBerserker
You shouldn't be writing code to suit your IDE - the IDE is supposed to support you. If you're having trouble with it, then switch. There are plenty of editors that deal with Python properly.Bogard
self.cfgAutomata has a type of list. Are you actually trying to get the type of self.cfgAutomata[<index>]?Mcandrew
There's a typo here, right? Misssing a comma in the __init__(): def __init__(self, params):Cascara
H
5

Python is strongly typed and Python lists are too. Your problem come from the fact that Python is dynamically typed. Therefor a var can contain any type, and therefor no IDE can guess what is the type of your parameter, nor give you code completion for the methods.

This is how it is, there is no clean workaround. If it's a problem, then maybe dynamics language is not you predilection tool and you should use something that fit your development style. There are tools for everybody.

Happiness answered 18/8, 2009 at 11:56 Comment(0)
H
5

8 years later and we actually have a solution in Python 3.6.

PEP484 allows you to annotate your variables primarily for IDEs and linting:

Modifying @Hani's answer:

x : AutomataBranch = self.cfgAutomata[i] 

This is now picked up by any good IDE to highlight errors and allow autocomplete.

Hayse answered 3/4, 2018 at 10:47 Comment(3)
and if I'm doing a for x in arrayOfClasses how to you specify the class name, I'm trying to access the properties from array[i] and they are not autocompleted using vsCode with Python pluginHightest
@MauricioGraciaGutierrez With nothing good - use "#type:" or predeclare it. See #41641949Hayse
thanks, but what it worked is mentioned here https://mcmap.net/q/1772455/-how-to-specify-type-classname-when-using-for-to-iterate-a-python-listHightest
G
2

I think you mean to say "statically typed" instead of "strongly typed." Python is strongly typed. You just don't know what that type is at compile time.

With that said, you really need to abandon the idea that you're going to find any IDEs that work as well for Python as they do for Java or C#. Python's dynamic typing makes this difficult. In fact, I tend to find that powerful IDEs are more of a burden than a help.

Gahan answered 18/8, 2009 at 11:58 Comment(1)
It is 2022 and python IDEs (e.g. Pycharm) are no longer a burden but a fast smart help.Intima
D
-1

I think I found a good managable solution. Actually it is trivial but may help (I used it now). When I want to access the list then I assign the object which I want to access to a variable ex:

x = AutomataBranch() 
x = self.cfgAutomata[i] 

The first line is used only to make the IDE knows that x is from AutomatBranch type. After that when I press x then all methods and properties are visualized.

I think it is some how good.

Derickderide answered 18/8, 2009 at 11:43 Comment(2)
This is a very bad idea. Coding in a special way to make autocompletion works, this is CRAZY.Happiness
Perhaps you are right but the problem lies beyond the fact that i am always using Microsoft Visual Studio and suddenly changing to a new IDE is not that easy.Of course i am going to refactor my code after getting it works. Thanks for your advice. Hani Almousli....Derickderide

© 2022 - 2024 — McMap. All rights reserved.