add dict.pop

This commit is contained in:
blueloveTH 2023-06-05 13:02:42 +08:00
parent 274f74818f
commit 940159e1ab
3 changed files with 16 additions and 1 deletions

View File

@ -117,3 +117,5 @@ class defaultdict:
def items(self):
return self._a.items()
def pop(self, key):
return self._a.pop(key)

View File

@ -898,6 +898,14 @@ inline void init_builtins(VM* _vm) {
self.erase(key);
});
_vm->bind_method<1>("dict", "pop", [](VM* vm, ArgsView args) {
Dict& self = _CAST(Dict&, args[0]);
PyObject* value = self.try_get(args[1]);
if(value == nullptr) vm->KeyError(args[1]);
self.erase(args[1]);
return value;
});
_vm->bind__contains__(_vm->tp_dict, [](VM* vm, PyObject* obj, PyObject* key) {
Dict& self = _CAST(Dict&, obj);
return self.contains(key);

View File

@ -47,4 +47,9 @@ assert d1 == d2
assert d1 != d3
a = dict([(1, 2), (3, 4)])
assert a == {1: 2, 3: 4}
assert a == {1: 2, 3: 4}
assert a.pop(1) == 2
assert a == {3: 4}
assert a.pop(3) == 4
assert a == {}