del_attr.py 619 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. class C:
  2. def f():
  3. pass
  4. # del a class attribute
  5. del C.f
  6. try:
  7. print(C.x)
  8. except AttributeError:
  9. print("AttributeError")
  10. try:
  11. del C.f
  12. except AttributeError:
  13. print("AttributeError")
  14. # del an instance attribute
  15. c = C()
  16. c.x = 1
  17. print(c.x)
  18. del c.x
  19. try:
  20. print(c.x)
  21. except AttributeError:
  22. print("AttributeError")
  23. try:
  24. del c.x
  25. except AttributeError:
  26. print("AttributeError")
  27. # try to del an attribute of a built-in class
  28. try:
  29. del int.to_bytes
  30. except (AttributeError, TypeError):
  31. # uPy raises AttributeError, CPython raises TypeError
  32. print('AttributeError/TypeError')