summaryrefslogtreecommitdiff
path: root/tests/basics/builtin_property.py
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2015-04-04 22:05:30 +0100
committerDamien George <damien.p.george@gmail.com>2015-04-04 22:05:30 +0100
commit9dd36404646f857c4f250537bac0d9a8ad041d25 (patch)
treec6509bcd3c7d5c2e67332110c582df2b5a5c669f /tests/basics/builtin_property.py
parent7e758b1cf878312cab5d9d2825b36e7235ea10a3 (diff)
tests: Add missing tests for builtins, and many other things.
Diffstat (limited to 'tests/basics/builtin_property.py')
-rw-r--r--tests/basics/builtin_property.py78
1 files changed, 78 insertions, 0 deletions
diff --git a/tests/basics/builtin_property.py b/tests/basics/builtin_property.py
new file mode 100644
index 000000000..4df9842a3
--- /dev/null
+++ b/tests/basics/builtin_property.py
@@ -0,0 +1,78 @@
+# test builtin property
+
+# create a property object explicitly
+property()
+property(1, 2, 3)
+
+# use its accessor methods
+p = property()
+p.getter(1)
+p.setter(2)
+p.deleter(3)
+
+# basic use as a decorator
+class A:
+ def __init__(self, x):
+ self._x = x
+
+ @property
+ def x(self):
+ print("x get")
+ return self._x
+
+a = A(1)
+print(a.x)
+
+try:
+ a.x = 2
+except AttributeError:
+ print("AttributeError")
+
+# explicit use within a class
+class B:
+ def __init__(self, x):
+ self._x = x
+
+ def xget(self):
+ print("x get")
+ return self._x
+
+ def xset(self, value):
+ print("x set")
+ self._x = value
+
+ def xdel(self):
+ print("x del")
+
+ x = property(xget, xset, xdel)
+
+b = B(3)
+print(b.x)
+b.x = 4
+print(b.x)
+del b.x
+
+# full use as a decorator
+class C:
+ def __init__(self, x):
+ self._x = x
+
+ @property
+ def x(self):
+ print("x get")
+ return self._x
+
+ @x.setter
+ def x(self, value):
+ print("x set")
+ self._x = value
+
+ @x.deleter
+ def x(self):
+ print("x del")
+
+c = C(5)
+print(c.x)
+c.x = 6
+print(c.x)
+del c.x