Created
February 4, 2019 07:29
-
-
Save jojonki/40c10986c5e57c4148b9675270548b18 to your computer and use it in GitHub Desktop.
Hack for read-only class variables.
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
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