Created
December 26, 2022 11:56
-
-
Save hamaguchi-amago/5b5125999a38ac325b891e5790577b94 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Constant types in Python. | |
e.g., http://code.activestate.com/recipes/414140-constant-types-in-python/ | |
""" | |
import sys | |
class _const: | |
class ConstError(TypeError): | |
pass | |
def __setattr__(self, name, value): | |
if name in self.__dict__: | |
# raise self.ConstError(f"Can't rebind const {name}") | |
sys.stderr.write(f"Can't rebind const {name}\n") | |
exit(0) | |
self.__dict__[name] = value | |
import sys | |
sys.modules[__name__] = _const() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ブログで作成しました。
【Python】プログラム中に定数を扱う注意点
https://neko-py.com/python-constants