簡體   English   中英

如何在Python中指定文字生成器類型?

[英]How do I specify the literal generator type in Python?

我需要檢查某個變量是否是生成器對象。 我如何指定文字生成器類型代替??? 下面?

def go():
    for i in range(999):
    yield i
la = go()
print repr(type(la))

<type 'generator'>

assert type(la) == ???

使用types.GeneratorType (來自types模塊)。 但是,您應該考慮為什么要這樣做。 通常最好避免顯式的類型檢查,而只是嘗試遍歷該對象並查看其是否有效。

import types
assert isinstance(la, types.GeneratorType)

一般來說,您不會。 您將查找名稱均為__iter__next函數。

暫無
暫無

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

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