forked from daleathan/ProgrammingInPython3-MarkSummerfield
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConst.py
More file actions
executable file
·53 lines (46 loc) · 1.73 KB
/
Const.py
File metadata and controls
executable file
·53 lines (46 loc) · 1.73 KB
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
#!/usr/bin/env python3
# Copyright (c) 2008-11 Qtrac Ltd. All rights reserved.
# This program or module is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version. It is provided for educational
# purposes and is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
class Const:
"""
>>> const = Const()
>>> const.text = "verified"
>>> const.limit = 591
>>> const.text, const.limit
('verified', 591)
>>> const.limit -= 12
Traceback (most recent call last):
...
ValueError: cannot change a const attribute
>>> const.x
Traceback (most recent call last):
...
AttributeError: 'Const' object has no attribute 'x'
>>> del const.text
Traceback (most recent call last):
...
ValueError: cannot delete a const attribute
>>> del const.x
Traceback (most recent call last):
...
AttributeError: 'Const' object has no attribute 'x'
"""
def __setattr__(self, name, value):
if name in self.__dict__:
raise ValueError("cannot change a const attribute")
self.__dict__[name] = value
def __delattr__(self, name):
if name in self.__dict__:
raise ValueError("cannot delete a const attribute")
raise AttributeError("'{0}' object has no attribute '{1}'"
.format(self.__class__.__name__, name))
if __name__ == "__main__":
import doctest
doctest.testmod()