pro*_*eek 12 python global-variables
我希望以在程序初始化代码中只设置一次的方式控制全局变量(或全局范围变量),并在此之后锁定它们.
我使用UPPER_CASE_VARIABLES作为全局变量,但我想确定无论如何都不要更改变量.
mar*_*eau 16
Activestate有一个名为Alex Constelli的Python中的常量,用于创建一个具有在创建后无法反弹的属性的模块.这听起来像你正在寻找的除了upcasing - 但可以通过检查属性名称是否全部大写来添加.const
当然,这可以通过确定的方式来规避,但这就是Python的方式 - 并且被大多数人认为是"好事".但是,为了使它更难一点,我建议你不要费心添加所谓明显的__delattr__方法,因为人们可以只删除名称,然后将它们添加回不同的值.
这是我正在采取的:
# Put in const.py...
# from http://code.activestate.com/recipes/65207-constants-in-python
class _const:
class ConstError(TypeError): pass # Base exception class.
class ConstCaseError(ConstError): pass
def __setattr__(self, name, value):
if name in self.__dict__:
raise self.ConstError("Can't change const.%s" % name)
if not name.isupper():
raise self.ConstCaseError('const name %r is not all uppercase' % name)
self.__dict__[name] = value
# Replace module entry in sys.modules[__name__] with instance of _const
# (and create additional reference to it to prevent its deletion -- see
# https://stackoverflow.com/questions/5365562/why-is-the-value-of-name-changing-after-assignment-to-sys-modules-name)
import sys
_ref, sys.modules[__name__] = sys.modules[__name__], _const()
if __name__ == '__main__':
import __main__ as const # Test this module...
try:
const.Answer = 42 # Not OK to create mixed-case attribute name.
except const.ConstCaseError as exc:
print(exc)
else: # Test failed - no ConstCaseError exception generated.
raise RuntimeError("Mixed-case const names should't be allowed!")
try:
const.ANSWER = 42 # Should be OK, all uppercase.
except Exception as exc:
raise RuntimeError("Defining a valid const attribute should be allowed!")
else: # Test succeeded - no exception generated.
print('const.ANSWER set to %d raised no exception' % const.ANSWER)
try:
const.ANSWER = 17 # Not OK, attempt to change defined constant.
except const.ConstError as exc:
print(exc)
else: # Test failed - no ConstError exception generated.
raise RuntimeError("Shouldn't be able to change const attribute!")
Run Code Online (Sandbox Code Playgroud)
输出:
const name 'Answer' is not all uppercase
const.ANSWER set to 42 raised no exception
Can't change const.ANSWER
Run Code Online (Sandbox Code Playgroud)
Python是一种非常开放的语言,不包含final关键字.Python让您可以更自由地做事,并假设您知道事情应该如何运作.因此,假设使用您的代码的人知道不SOME_CONSTANT应该在代码中的某个随机点分配值.
如果你真的想要,可以将常量包含在getter函数中.
def getConstant()
return "SOME_VALUE"
Run Code Online (Sandbox Code Playgroud)