看板 Python 關於我們 聯絡資訊
這個問題我也不曉得這樣問對不對 int 是 type object 的 instance 所以看int 的 type 會是 type,int 是 type object >>> type(int) <type 'type'> 看數值2 的 type 會是 int, 2 是 int object >>> type(2) <type 'int'> 我的問題是 function object 是誰的instance? 我不曉得我的問題對不對,或許我該問的是 function object 是誰的 subclass 因為我想知道 function object 是從哪來的 我用下面的程式碼定義了一個function object a >>> def a():pass >>> type(a) <type 'function'> >>> id(a) 4437405896 然後我很愚蠢的以為type(def)會有東西 >>> type(def) File "<stdin>", line 1 type(def) ^ SyntaxError: invalid syntax 也很愚蠢的以為type(function)會有東西 >>> type(function) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'function' is not defined >>> 那 function object 到底是從哪裡來的@@ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 123.194.223.76
Prius:help('def') 07/07 08:30
Prius:def是用來定義user-defined函數的"命令" 07/07 08:32
os653:非常沒信心的猜測,是從 Python source code 裡來的 07/07 08:58
os653:記憶有誤XD 也有可能是從 types.FunctionType 來的 07/07 09:03
os653:印象中 types 裡面的東西有些不能產生物件,不知是否記錯 07/07 09:09
os653:當初想抄 pickle 時遇過類似問題,有些東西就是無法 dump 07/07 09:12
tiefblau:痾... 這樣試試? type(type(a)) ---> <type 'type'> 07/07 15:00