11
import inspect
class Foo(object):
pass
if __name__ == '__main__':
print type(Foo.__init__)
print inspect.getsourcelines(Foo.__init__)
輸出:什麼是wrapper_descriptor,在這種情況下爲什麼是Foo .__ init __()?
<type 'wrapper_descriptor'>
Traceback (most recent call last):
*snip*
File "/usr/lib/python2.7/inspect.py", line 420, in getfile
'function, traceback, frame, or code object'.format(object))
TypeError: <slot wrapper '__init__' of 'object' objects> is not a module, class, method, function, traceback, frame, or code object
谷歌搜索提供了究竟,一個wrapper_descriptor是什麼,很少有用的信息,爲什麼一個空類有一個__init__
方法不是方法,而是wrapper_descriptor。
這裏究竟發生了什麼?沒有__init__
方法的所有課程都有這些wrapper_descriptor的東西之一嗎?爲什麼在班級字典中有__init__
?
如果您在意,C API文檔將解釋除此之外的一些細節。但通常你並不關心「插槽包裝器」,「方法包裝器」等類似的東西,你可以調用它們,就好像它們是綁定方法,未綁定方法等一樣,但是沒有任何Python源代碼。 – abarnert 2013-03-19 23:38:32