From 28370c04509a6255cd3d6b9424443a5b57bb7467 Mon Sep 17 00:00:00 2001 From: Andrew Leech Date: Wed, 10 Jun 2020 10:45:24 +1000 Subject: py/objtype: Add __dict__ attribute for class objects. The behavior mirrors the instance object dict attribute where a copy of the local attributes are provided (unless the dict is read-only, then that dict itself is returned, as an optimisation). MicroPython does not support modifying this dict because the changes will not be reflected in the class. The feature is only enabled if MICROPY_CPYTHON_COMPAT is set, the same as the instance version. --- tests/basics/class_dict.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 tests/basics/class_dict.py (limited to 'tests') diff --git a/tests/basics/class_dict.py b/tests/basics/class_dict.py new file mode 100644 index 000000000..f80ded678 --- /dev/null +++ b/tests/basics/class_dict.py @@ -0,0 +1,19 @@ +# test __dict__ attribute of a class + +if not hasattr(int, "__dict__"): + print("SKIP") + raise SystemExit + + +# dict of a built-in type +print("from_bytes" in int.__dict__) + + +# dict of a user class +class Foo: + a = 1 + b = "bar" + + +d = Foo.__dict__ +print(d["a"], d["b"]) -- cgit v1.2.3