/
ConstantPropertyByClosure.py
executable file
·57 lines (47 loc) · 1.24 KB
/
ConstantPropertyByClosure.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#! /usr/bin/env python
import copy
import pprint
import types
import sys
def ConstantProperty(value):
"""Create a closure protected constant copy of value."""
cache = copy.deepcopy(value)
return property(
lambda self: copy.deepcopy(cache)
)
class A(object):
def __init__(self, value):
"""Class constructor."""
A.constant = ConstantProperty(value)
def set_constant(self, value):
"""A non property setter for constant."""
self.constant = value
mutable = {
'A': [1, 2],
'B': 'cannot be change'
}
# Initialize object with a mutable object
a = A(mutable)
# Print object
pprint.pprint(a.constant)
# Try to change constant property
# by updating object referred by "value"
mutable['B'] = 'can be changed'
pprint.pprint(a.constant)
# Try to change constant property
# by updating object return by a.constant
a.constant['B'] = 'can be changed'
pprint.pprint(a.constant)
# Try to modify constant property
try:
a.constant = { 'A': 'new' }
pprint.pprint(a.constant)
except Exception as error:
print(error)
# Try to modify constant property
# from a class member function
try:
a.set_constant({ 'A': 'new' })
pprint.pprint(a.constant)
except Exception as error:
print(error)