Method 1
# -*- coding:utf-8 -*-
class Test(object):
def __init__(self):
self._x = None
def getx(self):
print "getx."
return self._x
def setx(self, value):
print "setx."
self._x = value
def delx(self):
print "delx."
del self._x
x = property(getx, setx, delx)
if __name__ == "__main__":
test = Test()
test.x = 1
test.x
del test.x
Execution result
setx.
getx.
delx.
Method 2
# -*- coding:utf-8 -*-
class Test(object):
def __init__(self):
self._x = None
@property
def x(self):
print "property x."
return self._x
@x.setter
def x(self, value):
print "setter x."
self._x = value
@x.deleter
def x(self):
print "deleter x."
del self._x
if __name__ == "__main__":
test = Test()
test.x = 1
test.x
del test.x
Execution result
setter x.
property x.
deleter x.
Recommended Posts