在Python中设置只读属性?

在Python中设置只读属性?,第1张

在Python中设置只读属性

尽管具有动态功能,但Python不允许猴子修补内置类型,例如

file
。它甚至可以通过修改
__dict__
这种类型的来防止您这样做—该
__dict__
属性返回包装在只读代理中的字典,因此分配
file.write
file.__dict__['write']
失败都会失败。至少有两个很好的理由:

  1. C代码期望

    file
    内置类型与
    PyFile
    类型结构以及内部使用
    file.write
    PyFile_Write()
    函数相对应。

  2. 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')


欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/zaji/5644882.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-16
下一篇2022-12-16

发表评论

登录后才能评论

评论列表(0条)

    保存