[問題] 請教一個很蠢的問題@@

看板Python作者 (hmm)時間12年前 (2013/07/07 00:24), 編輯推噓2(205)
留言7則, 3人參與, 最新討論串1/2 (看更多)
這個問題我也不曉得這樣問對不對 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

07/07 08:30, , 1F
help('def')
07/07 08:30, 1F

07/07 08:32, , 2F
def是用來定義user-defined函數的"命令"
07/07 08:32, 2F

07/07 08:58, , 3F
非常沒信心的猜測,是從 Python source code 裡來的
07/07 08:58, 3F

07/07 09:03, , 4F
記憶有誤XD 也有可能是從 types.FunctionType 來的
07/07 09:03, 4F

07/07 09:09, , 5F
印象中 types 裡面的東西有些不能產生物件,不知是否記錯
07/07 09:09, 5F

07/07 09:12, , 6F
當初想抄 pickle 時遇過類似問題,有些東西就是無法 dump
07/07 09:12, 6F

07/07 15:00, , 7F
痾... 這樣試試? type(type(a)) ---> <type 'type'>
07/07 15:00, 7F
文章代碼(AID): #1HsBKvGW (Python)
文章代碼(AID): #1HsBKvGW (Python)