2015-04-04 17:05:30 -04:00
|
|
|
# test builtin property
|
2017-03-08 18:07:19 -05:00
|
|
|
try:
|
|
|
|
property
|
|
|
|
except:
|
|
|
|
print("SKIP")
|
2017-06-10 13:03:01 -04:00
|
|
|
raise SystemExit
|
2015-04-04 17:05:30 -04:00
|
|
|
|
|
|
|
# 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
|
2014-04-13 14:00:27 -04:00
|
|
|
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")
|
|
|
|
|
2015-04-04 17:05:30 -04:00
|
|
|
# explicit use within a class
|
2014-04-13 14:00:27 -04:00
|
|
|
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
|
|
|
|
|
2015-04-04 17:05:30 -04:00
|
|
|
def xdel(self):
|
|
|
|
print("x del")
|
|
|
|
|
|
|
|
x = property(xget, xset, xdel)
|
2014-04-13 14:00:27 -04:00
|
|
|
|
|
|
|
b = B(3)
|
|
|
|
print(b.x)
|
|
|
|
b.x = 4
|
|
|
|
print(b.x)
|
2015-04-04 17:05:30 -04:00
|
|
|
del b.x
|
2014-04-13 14:00:27 -04:00
|
|
|
|
2015-04-04 17:05:30 -04:00
|
|
|
# full use as a decorator
|
2014-04-13 14:00:27 -04:00
|
|
|
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
|
|
|
|
|
2015-04-04 17:05:30 -04:00
|
|
|
@x.deleter
|
|
|
|
def x(self):
|
|
|
|
print("x del")
|
|
|
|
|
2014-04-13 14:00:27 -04:00
|
|
|
c = C(5)
|
|
|
|
print(c.x)
|
|
|
|
c.x = 6
|
|
|
|
print(c.x)
|
2015-04-04 17:05:30 -04:00
|
|
|
del c.x
|
2015-08-21 06:56:14 -04:00
|
|
|
|
|
|
|
# a property that has no get, set or del
|
|
|
|
class D:
|
|
|
|
prop = property()
|
|
|
|
d = D()
|
|
|
|
try:
|
|
|
|
d.prop
|
|
|
|
except AttributeError:
|
|
|
|
print('AttributeError')
|
|
|
|
try:
|
|
|
|
d.prop = 1
|
|
|
|
except AttributeError:
|
|
|
|
print('AttributeError')
|
|
|
|
try:
|
|
|
|
del d.prop
|
|
|
|
except AttributeError:
|
|
|
|
print('AttributeError')
|
2016-01-13 06:29:17 -05:00
|
|
|
|
|
|
|
# properties take keyword arguments
|
|
|
|
class E:
|
|
|
|
p = property(lambda self: 42, doc="This is truth.")
|
|
|
|
# not tested for because the other keyword arguments are not accepted
|
|
|
|
# q = property(fget=lambda self: 21, doc="Half the truth.")
|
|
|
|
print(E().p)
|
2018-05-24 20:59:40 -04:00
|
|
|
|
|
|
|
# a property as an instance member should not be delegated to
|
|
|
|
class F:
|
|
|
|
def __init__(self):
|
|
|
|
self.prop_member = property()
|
|
|
|
print(type(F().prop_member))
|