summaryrefslogtreecommitdiffstatshomepage
path: root/tests/misc/cexample_subclass.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/misc/cexample_subclass.py')
-rw-r--r--tests/misc/cexample_subclass.py37
1 files changed, 37 insertions, 0 deletions
diff --git a/tests/misc/cexample_subclass.py b/tests/misc/cexample_subclass.py
new file mode 100644
index 0000000000..9f52a2c737
--- /dev/null
+++ b/tests/misc/cexample_subclass.py
@@ -0,0 +1,37 @@
+# test subclassing custom native class
+
+try:
+ from cexample import AdvancedTimer
+except ImportError:
+ print("SKIP")
+ raise SystemExit
+
+
+class SubTimer(AdvancedTimer):
+ def __init__(self):
+ # At this point, self does not yet represent a AdvancedTimer instance.
+ print(self)
+
+ # So lookups via type.attr handler will fail.
+ try:
+ self.seconds
+ except AttributeError:
+ print("AttributeError")
+
+ # Also applies to builtin methods.
+ try:
+ self.time()
+ except AttributeError:
+ print("AttributeError")
+
+ # Initialize base class.
+ super().__init__(self)
+
+ # Now you can access methods and attributes normally.
+ self.time()
+ print(self.seconds)
+ self.seconds = 123
+ print(self.seconds)
+
+
+watch = SubTimer()