我试图在Python中定义一个只读属性的类; 我按照Python文档编写了以下代码:
#!/usr/bin/python
class Test:
def __init__(self, init_str):
self._prop = init_str
@property
def prop(self):
return self._prop
t = Test("Init")
print t.prop
t.prop = "Re-Init"
print t.prop
Run Code Online (Sandbox Code Playgroud)
现在当我尝试执行代码虽然我期待错误/异常时,我看到它正常执行:
$ ./python_prop_test
Init
Re-Init
Run Code Online (Sandbox Code Playgroud)
我的Python版本是2.7.2.我所看到的,是否有望?如何确保房产不可设置?