2016-06-28 53 views
0

Intellisence无法帮助对象的Python成员,因为只有在运行时才能知道对象类型。有没有办法指定变量的类型?在Python中定义强类型变量

E.g.

import xml.etree.ElementTree 

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

有写类似的方式:

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

我会建议使用Python的一个更好的文本编辑器。另外,python是强类型的,但不是静态类型。 –

+0

Pycharm IDE来拯救! – felipsmartins

+0

我正在使用PyCharm/IntelliJ – Bishoy

回答

2

有办法迫使香草PyCharm想起来的东西,但是,它招致的开销:

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

通过将它包装在if isinstance()中,PyCharm会推断它的类型并让你使用自动完成。

这不是理想的,但是这是Python的耸肩