[問題] 如何知道 class member 的type

看板Python作者 ( )時間15年前 (2010/05/27 22:19), 編輯推噓0(005)
留言5則, 4人參與, 最新討論串1/1
初學python 一直不知道怎樣知道class member 的type ex: http://tinyurl.com/34n8p2n class YouTubeVideoEntry(gdata.GDataEntry) __init__(self, author=None, category=None, content=None, atom_id=None, link=None, published=None, title=None, updated=None, rating=None, noembed=None, statistics=None, racy=None, media=None, geo=None, recorded=None, comments=None, extension_elements=None, extension_attributes=None) 怎麼會知道裡面的author, category, content 是什麼type 也許是list, tuple, class instance ..... ? 這樣如何繼續操作member ? thanks 假設說member 是type class A; 但是我根本不知道他是class A 的instance 那我在寫程式時候怎麼去使用class A的member, function ? 用type() ? 這也要等到runtime 才會知道~~~ 那寫程式時 程式員不是非常困擾嗎? -- -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 219.87.71.10

05/27 22:28, , 1F
type()?
05/27 22:28, 1F

05/27 22:28, , 2F
type(author) ?
05/27 22:28, 2F

05/27 22:31, , 3F
不過一般來說型別錯誤會丟exception吧...
05/27 22:31, 3F

05/27 23:46, , 4F
exception也要等用到了才會踢出來呀, 有時候有點麻煩
05/27 23:46, 4F
※ 編輯: phkmalloc 來自: 220.135.222.237 (05/28 06:59)

05/29 20:44, , 5F
isinstance or __class__
05/29 20:44, 5F
文章代碼(AID): #1B_dzd6y (Python)
文章代碼(AID): #1B_dzd6y (Python)