[問題] 如何用程式判斷某函數是否含有yield?

看板Python作者 ( hua)時間14年前 (2010/12/02 18:22), 編輯推噓1(105)
留言6則, 4人參與, 最新討論串1/1
如何能在不執行某函數的狀況下, 以程式判斷此函數含有yield? (即此函數可以成為iterator) -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 211.20.60.207

12/02 23:05, , 1F
type(function()) ?
12/02 23:05, 1F

12/02 23:38, , 2F
keyword: decompile, func_code, co_code, opcode, opname
12/02 23:38, 2F

12/03 00:19, , 3F
Catch errors rather than avoiding them
12/03 00:19, 3F

12/03 00:20, , 4F
to avoid cluttering your code with special cases.
12/03 00:20, 4F

12/03 00:40, , 5F
inspect.isgenerator(object)
12/03 00:40, 5F

12/03 11:17, , 6F
感謝ykjiang, inspect.isgeneratorfunction
12/03 11:17, 6F
文章代碼(AID): #1CztDigd (Python)
文章代碼(AID): #1CztDigd (Python)