py/objobject: Add object.__delattr__ function.

Similar to object.__setattr__.
This commit is contained in:
Yonatan Goldschmidt
2019-12-10 12:05:22 +02:00
committed by Damien George
parent 07ccb5588c
commit 42e45bd694
2 changed files with 43 additions and 0 deletions

View File

@@ -69,6 +69,9 @@ class C:
def __setattr__(self, attr, value):
print(attr, "=", value)
def __delattr__(self, attr):
print("del", attr)
c = C()
c.a = 5
try:
@@ -86,3 +89,25 @@ try:
object.__setattr__(c, 5, 5)
except TypeError:
print("TypeError")
# test object.__delattr__
del c.a
print(c.a)
object.__delattr__(c, "a")
try:
print(c.a)
except AttributeError:
print("AttributeError")
super(C, c).__delattr__("b")
try:
print(c.b)
except AttributeError:
print("AttributeError")
try:
object.__delattr__(c, "c")
except AttributeError:
print("AttributeError")