
尽管具有动态功能,但Python不允许猴子修补内置类型,例如
file。它甚至可以通过修改
__dict__这种类型的来防止您这样做—该
__dict__属性返回包装在只读代理中的字典,因此分配
file.write和
file.__dict__['write']失败都会失败。至少有两个很好的理由:
C代码期望
file
内置类型与PyFile
类型结构以及内部使用file.write
的PyFile_Write()
函数相对应。Python对类型实现属性访问的缓存,以加快方法查找和实例方法的创建。如果允许直接分配给类型dict,则此缓存将被破坏。
用Python实现的类当然可以使用猴子补丁,它可以很好地处理动态修改。
但是,如果您真的知道自己在做什么,则可以使用低级API,例如,
ctypes可以挂接到实现并获取类型dict。例如:
# WARNING: do NOT attempt this in production pre!import ctypesdef magic_get_dict(o): # find address of dict whose offset is stored in the type dict_addr = id(o) + type(o).__dictoffset__ # retrieve the dict object itself dict_ptr = ctypes.cast(dict_addr, ctypes.POINTER(ctypes.py_object)) return dict_ptr.contents.valuedef magic_flush_mro_cache(): ctypes.PyDLL(None).PyType_Modified(ctypes.py_object(object))# monkey-patch file.writedct = magic_get_dict(file)dct['write'] = lambda f, s, orig_write=file.write: orig_write(f, '42')# flush the method cache for the monkey-patch to take effectmagic_flush_mro_cache()# magic!import syssys.stdout.write('hello worldn')欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)