Skip to content

Instantly share code, notes, and snippets.

@jojonki
Created February 4, 2019 07:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jojonki/40c10986c5e57c4148b9675270548b18 to your computer and use it in GitHub Desktop.
Save jojonki/40c10986c5e57c4148b9675270548b18 to your computer and use it in GitHub Desktop.
Hack for read-only class variables.
class MetaGlobalVariables(type):
@property
def HOGE(cls):
return cls._GlobalVariables__hoge # mangling
class GlobalVariables(object, metaclass=MetaGlobalVariables):
__hoge = 'xxxx'
# read OK
# GlobalVariables.HOGE
# -> 'xxxx'
# prohibit to set
# GlobalVariables.HOGE = 'yyyy'
# AttributeError: can't set attribute
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment