python 中的属性嵌套与 setter 和 getter
nesting of properties vs setters and getters in python
class OurAtt():
def __init__(self):
self.Log = False
def setLog(self):
self.Log = True
def clearLog(self):
self.Log = False
class OurClass(object):
def __init__(self):
self.__OurAtt = OurAtt()
@property
def OurAtt(self):
return self.__OurAtt
@OurAtt.setter
def OurAtt(self, val):
raise Exception("can't modify the attribute" )
x = OurClass()
x.OurAtt.setLog()
print x.OurAtt.Log # print True
x.OurAtt.Log = False
print x.OurAtt.Log # sets to False Aim set this through function call x.OurAtt.setLog() I want to restrict the access, something like private variable.
最终目标是Log 应该是OurAttr 的属性并且应该受到getter 和setter 或属性的保护。它就像属性的嵌套。层次结构应保持 object.OurAttr.Log
我研究并得到以下 link。
Python: multiple properties, one setter/getter
但是没有达到我的目的。
我实际上是 getter、setter 和属性的新手。提前致谢
我相信你是 over-complicating 的问题。如果你想阻止访问 OurAtt 的属性,@property
装饰器应该与 OurAtt
一起使用。 OurAtt
class 的实例将始终实现此 protected-access 行为,包括当它们是 OurClass
的成员时。你不需要对 OurClass
中的 @property
装饰器做任何事情,除非你想阻止修改那个 class.
的成员
我认为这可以完成您想要完成的工作。它在 2.7 下运行 - 如果您使用的是早期版本,您的里程可能会有所不同。
class OurAttr(object):
def __init__(self):
self._log = False
@property
def log(self):
return self._log
@log.setter
def log(self, value):
raise AttributeError("Cannot set 'log' attribute directly.")
@log.deleter
def log(self):
raise AttributeError("Cannot delete 'log' attribute directly.")
def setLog(self):
self._log = True
print "Log is", self._log
def clearLog(self):
self._log = False
print "Log is", self._log
class OurClass(object):
def __init__(self):
self.OurAttr = OurAttr()
oc = OurClass()
oc.OurAttr.setLog()
oc.OurAttr.clearLog()
oc.OurAttr.log = False # Raises exception
输出为:
$ python2.7 test.py
Log is True
Log is False
Traceback (most recent call last):
File "test.py", line 33, in <module>
oc.OurAttr.log = False
File "test.py", line 11, in log
raise AttributeError("Cannot set 'log' attribute directly.")
AttributeError: Cannot set 'log' attribute directly.
class OurAtt():
def __init__(self):
self.Log = False
def setLog(self):
self.Log = True
def clearLog(self):
self.Log = False
class OurClass(object):
def __init__(self):
self.__OurAtt = OurAtt()
@property
def OurAtt(self):
return self.__OurAtt
@OurAtt.setter
def OurAtt(self, val):
raise Exception("can't modify the attribute" )
x = OurClass()
x.OurAtt.setLog()
print x.OurAtt.Log # print True
x.OurAtt.Log = False
print x.OurAtt.Log # sets to False Aim set this through function call x.OurAtt.setLog() I want to restrict the access, something like private variable.
最终目标是Log 应该是OurAttr 的属性并且应该受到getter 和setter 或属性的保护。它就像属性的嵌套。层次结构应保持 object.OurAttr.Log
我研究并得到以下 link。
Python: multiple properties, one setter/getter
但是没有达到我的目的。
我实际上是 getter、setter 和属性的新手。提前致谢
我相信你是 over-complicating 的问题。如果你想阻止访问 OurAtt 的属性,@property
装饰器应该与 OurAtt
一起使用。 OurAtt
class 的实例将始终实现此 protected-access 行为,包括当它们是 OurClass
的成员时。你不需要对 OurClass
中的 @property
装饰器做任何事情,除非你想阻止修改那个 class.
我认为这可以完成您想要完成的工作。它在 2.7 下运行 - 如果您使用的是早期版本,您的里程可能会有所不同。
class OurAttr(object):
def __init__(self):
self._log = False
@property
def log(self):
return self._log
@log.setter
def log(self, value):
raise AttributeError("Cannot set 'log' attribute directly.")
@log.deleter
def log(self):
raise AttributeError("Cannot delete 'log' attribute directly.")
def setLog(self):
self._log = True
print "Log is", self._log
def clearLog(self):
self._log = False
print "Log is", self._log
class OurClass(object):
def __init__(self):
self.OurAttr = OurAttr()
oc = OurClass()
oc.OurAttr.setLog()
oc.OurAttr.clearLog()
oc.OurAttr.log = False # Raises exception
输出为:
$ python2.7 test.py
Log is True
Log is False
Traceback (most recent call last):
File "test.py", line 33, in <module>
oc.OurAttr.log = False
File "test.py", line 11, in log
raise AttributeError("Cannot set 'log' attribute directly.")
AttributeError: Cannot set 'log' attribute directly.