Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagesmc
Path: blob/master/src/sage/structure/mutability.pyx
8814 views
1
"""
2
Mutability Cython Implementation
3
"""
4
5
##########################################################################
6
#
7
# Sage: System for Algebra and Geometry Experimentation
8
#
9
# Copyright (C) 2006 William Stein <[email protected]>
10
#
11
# Distributed under the terms of the GNU General Public License (GPL)
12
# http://www.gnu.org/licenses/
13
##########################################################################
14
15
16
cdef class Mutability:
17
18
def __init__(self, is_immutable=False):
19
self._is_immutable = is_immutable
20
21
def _require_mutable(self):
22
if self._is_immutable:
23
raise ValueError, "object is immutable; please change a copy instead."
24
25
cdef _require_mutable_cdef(self):
26
if self._is_immutable:
27
raise ValueError, "object is immutable; please change a copy instead."
28
29
def set_immutable(self):
30
"""
31
Make this object immutable, so it can never again be changed.
32
33
EXAMPLES::
34
35
sage: v = Sequence([1,2,3,4/5])
36
sage: v[0] = 5
37
sage: v
38
[5, 2, 3, 4/5]
39
sage: v.set_immutable()
40
sage: v[3] = 7
41
Traceback (most recent call last):
42
...
43
ValueError: object is immutable; please change a copy instead.
44
"""
45
self._is_immutable = 1
46
47
def is_immutable(self):
48
"""
49
Return True if this object is immutable (can not be changed)
50
and False if it is not.
51
52
To make this object immutable use self.set_immutable().
53
54
EXAMPLE::
55
56
sage: v = Sequence([1,2,3,4/5])
57
sage: v[0] = 5
58
sage: v
59
[5, 2, 3, 4/5]
60
sage: v.is_immutable()
61
False
62
sage: v.set_immutable()
63
sage: v.is_immutable()
64
True
65
"""
66
self._is_immutable
67
68
def is_mutable(self):
69
return not self._is_immutable
70
71
def __reduce__(self):
72
return Mutability, (self._is_immutable, )
73
74
#################################################################################
75
## Method decorators for mutating methods resp. methods that assume immutability
76
from sage.misc.decorators import sage_wraps
77
78
def require_mutable(f):
79
"""
80
A decorator that requires mutability for a method to be called.
81
82
EXAMPLES::
83
84
sage: from sage.structure.mutability import require_mutable, require_immutable
85
sage: class A:
86
... def __init__(self, val):
87
... self._m = val
88
... @require_mutable
89
... def change(self, new_val):
90
... 'change self'
91
... self._m = new_val
92
... @require_immutable
93
... def __hash__(self):
94
... 'implement hash'
95
... return hash(self._m)
96
sage: a = A(5)
97
sage: a.change(6)
98
sage: hash(a)
99
Traceback (most recent call last):
100
...
101
ValueError: <type 'instance'> instance is mutable, <function __hash__ at ...> must not be called
102
sage: a._is_immutable = True
103
sage: hash(a)
104
6
105
sage: a.change(7) # indirect doctest
106
Traceback (most recent call last):
107
...
108
ValueError: <type 'instance'> instance is immutable, <function change at ...> must not be called
109
sage: from sage.misc.sageinspect import sage_getdoc
110
sage: print sage_getdoc(a.change)
111
change self
112
113
AUTHORS:
114
115
- Simon King <[email protected]>
116
"""
117
@sage_wraps(f)
118
def new_f(self, *args,**kwds):
119
if getattr(self,'_is_immutable',False):
120
raise ValueError("%s instance is immutable, %s must not be called"%(type(self), repr(f)))
121
return f(self, *args,**kwds)
122
return new_f
123
124
def require_immutable(f):
125
"""
126
A decorator that requires mutability for a method to be called.
127
128
EXAMPLES::
129
130
sage: from sage.structure.mutability import require_mutable, require_immutable
131
sage: class A:
132
... def __init__(self, val):
133
... self._m = val
134
... @require_mutable
135
... def change(self, new_val):
136
... 'change self'
137
... self._m = new_val
138
... @require_immutable
139
... def __hash__(self):
140
... 'implement hash'
141
... return hash(self._m)
142
sage: a = A(5)
143
sage: a.change(6)
144
sage: hash(a) # indirect doctest
145
Traceback (most recent call last):
146
...
147
ValueError: <type 'instance'> instance is mutable, <function __hash__ at ...> must not be called
148
sage: a._is_immutable = True
149
sage: hash(a)
150
6
151
sage: a.change(7)
152
Traceback (most recent call last):
153
...
154
ValueError: <type 'instance'> instance is immutable, <function change at ...> must not be called
155
sage: from sage.misc.sageinspect import sage_getdoc
156
sage: print sage_getdoc(a.__hash__)
157
implement hash
158
159
AUTHORS:
160
161
- Simon King <[email protected]>
162
"""
163
@sage_wraps(f)
164
def new_f(self, *args,**kwds):
165
if not getattr(self,'_is_immutable',False):
166
raise ValueError("%s instance is mutable, %s must not be called"%(type(self), repr(f)))
167
return f(self, *args,**kwds)
168
return new_f
169
170