source: sage/structure/mutability.py @ 0:039f6310c6fe

Revision 0:039f6310c6fe, 1.9 KB checked in by tornaria@…, 7 years ago (diff)

[project @ original sage-0.10.12]

Line 
1r"""
2Mutability
3"""
4
5##########################################################################
6#
7#   SAGE: System for Algebra and Geometry Experimentation   
8#
9#       Copyright (C) 2006 William Stein <wstein@ucsd.edu>
10#
11#  Distributed under the terms of the GNU General Public License (GPL)
12#                  http://www.gnu.org/licenses/
13##########################################################################
14
15class Mutability:
16    def __init__(self, is_immutable=False):
17        self._is_immutable = is_immutable
18       
19    def _require_mutable(self):
20        if self._is_immutable:
21            raise TypeError, "%s is immutable; please change a copy instead."%self
22
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            TypeError: frozen sequences cannot be changed
37        """
38        self._is_immutable = True
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
Note: See TracBrowser for help on using the repository browser.