如何在Python中“锁定”对象?将可变对象锁定为python中的不可变
说我有:
class Foo:
def __init__(self):
self.bar = []
self.qnx = 10
我会修改foo的,就像我想:
foo = Foo()
foo.bar.append('blah')
foo.qnx = 20
但后来我想能够“锁住”,这样,当我试试
lock(foo)
foo.bar.append('blah') # raises some exception.
foo.qnx = 20 # raises some exception.
Python中可能吗?
好问题;也许这个帖子是相关的? http://stackoverflow.com/questions/4828080/how-to-make-an-immutable-object-in-python –
你的问题是不连贯的。看:'t = [2,3]; foo.bar = t;锁(FOO); t.append(4)' - 它应该工作,还是应该引发异常? – Veky