2016-06-28 98 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的聳肩