簡體   English   中英

如何在python中鍵入提示組合類的類型列表?

[英]How do I type hint a list of types for a composite class in python?

由於無法控制的某些體系結構原因,我經常使用並希望完整代碼完成的對象是源代碼中已存在的靜態功能之上的多個功能的動態組合。

import lgb.reqs.plan
# Various imports which dynamically extend the smallform
import lgb_extensions.water_extras
import lgb_extensions.toolkit_extras

d = c.req[0]  # type: lgb.reqs.plan.smallform 
d = d  # type: lgb_extensions.water_extras.common
d = d  # type: lgb_extensions.toolkit_extras.common

# Now I get the autocomplete on d as I type "d."
d.

我已經找到了重新分配d的方法來工作的偉大,但感覺不對。 有沒有辦法用元組或其他東西鍵入提示? 我嘗試過,無法解決。

我發現jupyter notebook非常適合自動完成,如果我真的需要探索未知的代碼庫,我將跳入ipython或Notebook會話,但是在這種情況下,我對代碼庫非常熟悉,並且我希望自動完成會更好,因為我永遠不記得那叫什么。 如果重要的話,我主要在pycharm或atom中。 如果只有幾個擴展 ,上面的解決方案已經解決了我的問題,但是當我有10個擴展對象的東西時,它不起作用。 在我通常使用的情況下,我約有20件事擴展了想要自動完成的對象。

您也許可以在此處使用Union 更重要的是,一個名稱在不同情況下可以容納不同類型。

例如。

from typing import Union

a = f() # type: Union[str, int]
d. # now get autocompletion for str and int from IDEs

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM