summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorTim Peters <tim.peters@gmail.com>2001-09-24 21:17:50 (GMT)
committerTim Peters <tim.peters@gmail.com>2001-09-24 21:17:50 (GMT)
commit66c1a525e08fda439ddda3b6371236df1398cfd5 (patch)
tree9ac8e6d2fda99e406a40b281ae315d686a570c53 /Lib/test
parent30c484916988862608e4efdfa8f8aa911e4cc0c3 (diff)
downloadcpython-66c1a525e08fda439ddda3b6371236df1398cfd5.zip
cpython-66c1a525e08fda439ddda3b6371236df1398cfd5.tar.gz
cpython-66c1a525e08fda439ddda3b6371236df1398cfd5.tar.bz2
Make properties discoverable from Python:
- property() now takes 4 keyword arguments: fget, fset, fdel, doc. Note that the real purpose of the 'f' prefix is to make fdel fit in ('del' is a keyword, so can't used as a keyword argument name). - These map to visible readonly attributes 'fget', 'fset', 'fdel', and '__doc__' in the property object. - fget/fset/fdel weren't discoverable from Python before. - __doc__ is new, and allows to associate a docstring with a property.
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_descr.py30
1 files changed, 28 insertions, 2 deletions
diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py
index fdf2a41..5bd837e 100644
--- a/Lib/test/test_descr.py
+++ b/Lib/test/test_descr.py
@@ -1364,7 +1364,7 @@ def properties():
self.__x = value
def delx(self):
del self.__x
- x = property(getx, setx, delx)
+ x = property(getx, setx, delx, doc="I'm the x property.")
a = C()
verify(not hasattr(a, "x"))
a.x = 42
@@ -1378,6 +1378,32 @@ def properties():
## C.x.__set__(a)
## verify(not hasattr(a, "x"))
+ raw = C.__dict__['x']
+ verify(isinstance(raw, property))
+
+ attrs = dir(raw)
+ verify("__doc__" in attrs)
+ verify("fget" in attrs)
+ verify("fset" in attrs)
+ verify("fdel" in attrs)
+
+ verify(raw.__doc__ == "I'm the x property.")
+ verify(raw.fget is C.__dict__['getx'])
+ verify(raw.fset is C.__dict__['setx'])
+ verify(raw.fdel is C.__dict__['delx'])
+
+ for attr in "__doc__", "fget", "fset", "fdel":
+ try:
+ setattr(raw, attr, 42)
+ except TypeError, msg:
+ if str(msg).find('readonly') < 0:
+ raise TestFailed("when setting readonly attr %r on a "
+ "property, got unexpected TypeError "
+ "msg %r" % (attr, str(msg)))
+ else:
+ raise TestFailed("expected TypeError from trying to set "
+ "readonly %r attr on a property" % attr)
+
def supers():
if verbose: print "Testing super..."
@@ -1884,7 +1910,7 @@ def rich_comparisons():
zz = ZZ(1.0000003)
verify(zz == 1+0j)
verify(1+0j == zz)
-
+
class classic:
pass
for base in (classic, int, object, list):