Bishoy
Bishoy

Reputation: 725

Define strong-typed variables in Python

Intellisence can't able help with Python members of an object because the object type will only be known at runtime. Is there a way to specify the type of the variable?

E.g.

import xml.etree.ElementTree

root = xml.etree.ElementTree.parse(x).getroot()
for data in root.findall('./data'):
    data.

Is there a way to write something like:

    for data:xml.etree.Element in root.findall('./data'):

Upvotes: 1

Views: 562

Answers (1)

TemporalWolf
TemporalWolf

Reputation: 7952

There is a way to force vanilla PyCharm to think of it as something, however, it does incur overhead:

root = xml.etree.ElementTree.parse(x).getroot()
        for data in root.findall('./data'):
            if isinstance(data, xml.etree.ElementTree.Element):
                data.

By wrapping it in if isinstance(), PyCharm will infer it's type and let you use auto-completion.

It's not ideal, but that's Python shrug

Upvotes: 3

Related Questions