From 3dd16c6a387fa4d778722fdb16fd03e933bf4c45 Mon Sep 17 00:00:00 2001 From: Mike C <2045948+mikecee@users.noreply.github.com> Date: Mon, 4 Mar 2024 12:15:38 +0100 Subject: [PATCH] Fix handling of optional default value for .pop() (#47) --- easydict/__init__.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/easydict/__init__.py b/easydict/__init__.py index 78963c1..f4dcc1e 100644 --- a/easydict/__init__.py +++ b/easydict/__init__.py @@ -123,6 +123,12 @@ class EasyDict(dict): Traceback (most recent call last): ... AttributeError: 'EasyDict' object has no attribute 'a' + >>> d.pop('a', 8) + 8 + >>> d.pop('b', 100) + 4 + >>> d + {'c': 3.0} """ def __init__(self, d=None, **kwargs): if d is None: @@ -155,9 +161,10 @@ def update(self, e=None, **f): for k in d: setattr(self, k, d[k]) - def pop(self, k, d=None): - delattr(self, k) - return super(EasyDict, self).pop(k, d) + def pop(self, k, *args): + if hasattr(self, k): + delattr(self, k) + return super(EasyDict, self).pop(k, *args) if __name__ == "__main__":