Skip to content

Instantly share code, notes, and snippets.

@hamaguchi-amago
Created December 26, 2022 11:56
Show Gist options
  • Save hamaguchi-amago/5b5125999a38ac325b891e5790577b94 to your computer and use it in GitHub Desktop.
Save hamaguchi-amago/5b5125999a38ac325b891e5790577b94 to your computer and use it in GitHub Desktop.
"""
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()
@hamaguchi-amago
Copy link
Author

ブログで作成しました。

【Python】プログラム中に定数を扱う注意点
https://neko-py.com/python-constants

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment