Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/structure/mutability_py.py
4045 views
1
"""
2
Mutability -- Python Implementation
3
"""
4
5
##########################################################################
6
#
7
# Sage: System for Algebra and Geometry Experimentation
8
#
9
# Copyright (C) 2007 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
class Mutability:
16
17
def __init__(self, is_immutable=False):
18
self._is_immutable = is_immutable
19
20
def _require_mutable(self):
21
if self._is_immutable:
22
raise ValueError, "object is immutable; please change a copy instead."
23
def set_immutable(self):
24
"""
25
Make this object immutable, so it can never again be changed.
26
27
EXAMPLES:
28
sage: v = Sequence([1,2,3,4/5])
29
sage: v[0] = 5
30
sage: v
31
[5, 2, 3, 4/5]
32
sage: v.set_immutable()
33
sage: v[3] = 7
34
Traceback (most recent call last):
35
...
36
ValueError: object is immutable; please change a copy instead.
37
"""
38
self._is_immutable = 1
39
40
def is_immutable(self):
41
"""
42
Return True if this object is immutable (can not be changed)
43
and False if it is not.
44
45
To make this object immutable use self.set_immutable().
46
47
EXAMPLE:
48
sage: v = Sequence([1,2,3,4/5])
49
sage: v[0] = 5
50
sage: v
51
[5, 2, 3, 4/5]
52
sage: v.is_immutable()
53
False
54
sage: v.set_immutable()
55
sage: v.is_immutable()
56
True
57
"""
58
try:
59
return self._is_immutable
60
except AttributeError:
61
return False
62
63
def is_mutable(self):
64
try:
65
return not self._is_immutable
66
except AttributeError:
67
return True
68
69
def __reduce__(self):
70
return Mutability, (self._is_immutable, )
71
72
73