Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/structure/mutability.pyx
4045 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