我也不知道为啥没有显式的 set ,但是你可以自己实现一个
```python
import ctypes
class PyType(ctypes.Structure):
pass
class PyObject(ctypes.Structure):
Py_ssize_t = (
ctypes.c_int64 if ctypes.sizeof(ctypes.c_void_p) == 8 else ctypes.c_int32
)
_fields_ = [
("ob_refcnt", Py_ssize_t),
("ob_type", ctypes.POINTER(PyType)),
]
class PyTypeObject(PyObject):
_fields_ = [
("dict", ctypes.POINTER(PyObject))
]
def inject(class_, method, force=False):
def _(function):
name_, dict_ = class_.__name__, class_.__dict__
proxy_dict = PyTypeObject.from_address(id(dict_))
namespace = {}
ctypes.pythonapi.PyDict_SetItem(
ctypes.py_object(namespace),
ctypes.py_object(name_),
proxy_dict.dict
)
if not force and namespace.get(name_, {}).get(method, None):
raise RuntimeError(f"已存在方法 {class_.__name__}.{method}()")
namespace[name_][method] = function
return _
@
inject(dict, 'set')
def dict_set(d, key, value):
d.update({key: value})
```
a = {}
a.set("name", "hello")
print(a)