Home > Mobile >  Is there a way to show the IDE which Type of Objects are in a List, so it colors / autocompletes pro
Is there a way to show the IDE which Type of Objects are in a List, so it colors / autocompletes pro

Time:08-11

I recognize from c# that you could do

(int)(randomVariable 1 anythingElse)

and the IDE would understand that the named variable/result is an integer.

Now in Python I want to let my IDE know, that in the list ceDocs which I am sending as a parameter will be objects of the class Doc so I can use autocomplete and proper coloring instead of "any" everywhere.

def getFromPath(path):
    fileNames = glob.glob(path   "*")
    files = []
    for file in fileNames:
        files.append = Doc(file)
    return files

def createDocuments(ceDocs):
    for doc in ceDocs:
        doc.name = doc.path # example action

createDocuments(getFromPath(path))

Is there a way to do this in python?

Please correct me if this makes no sense... :)

CodePudding user response:

Use type hints with the typing module.

from typing import List

class Doc:
   # ...

def createDocuments(ceDocs: List[Doc]):
    for doc in ceDocs:
        doc.name = doc.path # example action

CodePudding user response:

For python 3.10:

def createDocuments(ceDocs: list[Doc]):
    for doc in ceDocs:
        doc.name = doc.path # example action

For earlier versions:

from typing import List

def createDocuments(ceDocs: List[Doc]):
    for doc in ceDocs:
        doc.name = doc.path # example action
  • Related