zfr*_*cus 903 python constants
有没有办法在Python中声明一个常量?在Java中,我们可以用这种方式创建常量值:
public static final String CONST_NAME = "Name";
Run Code Online (Sandbox Code Playgroud)
Python中上述Java常量声明的等价物是什么?
Fel*_*ing 887
不,那里没有.您不能在Python中将变量或值声明为常量.只是不要改变它.
如果你在一个班级,相当于:
class Foo(object):
CONST_NAME = "Name"
Run Code Online (Sandbox Code Playgroud)
如果没有,那就是
CONST_NAME = "Name"
Run Code Online (Sandbox Code Playgroud)
但是你可能想看看Alex Martelli的Python代码片段常量.
inv*_*inv 338
const在其他语言中没有关键字,但是可以创建一个具有"getter函数"来读取数据的Property ,但是没有"setter函数"来重写数据.这实质上保护标识符不被更改.
以下是使用class属性的替代实现:
请注意,对于想知道常量的读者而言,代码并不容易.见下面的解释
def constant(f):
def fset(self, value):
raise TypeError
def fget(self):
return f()
return property(fget, fset)
class _Const(object):
@constant
def FOO():
return 0xBAADFACE
@constant
def BAR():
return 0xDEADBEEF
CONST = _Const()
print CONST.FOO
##3131964110
CONST.FOO = 0
##Traceback (most recent call last):
## ...
## CONST.FOO = 0
##TypeError: None
Run Code Online (Sandbox Code Playgroud)
代码说明:
constant接受表达式的函数,并使用它来构造一个"getter" - 一个只返回表达式值的函数.constant我们刚刚创建的函数作为装饰来快速定义只读属性.而在其他一些更老式的方式:
(代码非常棘手,下面有更多解释)
class _Const(object):
@apply
def FOO():
def fset(self, value):
raise TypeError
def fget(self):
return 0xBAADFACE
return property(**locals())
CONST = _Const()
print CONST.FOO
##3131964110
CONST.FOO = 0
##Traceback (most recent call last):
## ...
## CONST.FOO = 0
##TypeError: None
Run Code Online (Sandbox Code Playgroud)
请注意,@ apply装饰器似乎已被弃用.
property函数构造一个可以"设置"或"获取"的对象.property函数的前两个参数是命名的fset和fget.property函数Anu*_*yal 103
在Python中,而不是语言强制执行某些东西,人们使用命名约定,例如__method私有方法和_method用于受保护的方法.
因此,以同样的方式,您可以简单地将常量声明为全部大写,例如
MY_CONSTANT = "one"
Run Code Online (Sandbox Code Playgroud)
如果你想要这个常量永远不会改变,你可以挂钩属性访问并做一些技巧,但更简单的方法是声明一个函数
def MY_CONSTANT():
return "one"
Run Code Online (Sandbox Code Playgroud)
只有问题无处不在,你必须做MY_CONSTANT(),但MY_CONSTANT = "one"在python(通常)中再次是正确的方法.
您还可以使用namedtuple来创建常量:
>>> from collections import namedtuple
>>> Constants = namedtuple('Constants', ['pi', 'e'])
>>> constants = Constants(3.14, 2.718)
>>> constants.pi
3.14
>>> constants.pi = 3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: can't set attribute
Run Code Online (Sandbox Code Playgroud)
Jon*_*tts 57
我可能在这里错过了一个技巧,但这似乎对我有用:
class CONST(object):
__slots__ = ()
FOO = 1234
CONST = CONST()
# ----------
print(CONST.FOO) # 1234
CONST.FOO = 4321 # AttributeError: 'CONST' object attribute 'FOO' is read-only
CONST.__dict__['FOO'] = 4321 # AttributeError: 'CONST' object has no attribute '__dict__'
CONST.BAR = 5678 # AttributeError: 'CONST' object has no attribute 'BAR'
Run Code Online (Sandbox Code Playgroud)
创建实例允许魔术__dict__方法启动并拦截设置__dict__变量的尝试.如果你愿意,可以在这里抛出异常.通过类名实例化实例可以防止直接通过类进行访问.
这对于一个值来说是一种痛苦,但是你可以将很多东西附加到你的__setattr__对象上.上流社会,班级名称似乎也有点粗鲁,但我认为整体而言非常简洁.
Sae*_*aig 29
你可能已经知道,Python没有常量:(
也许最简单的选择是为它定义一个函数.例如
def MY_CONSTANT():
return 42
Run Code Online (Sandbox Code Playgroud)
MY_CONSTANT() 现在具有常量的所有功能(加上一些恼人的大括号).
han*_*ine 19
除了两个最佳答案(只使用带有大写名称的变量,或使用属性使值只读),我想提一下,为了实现命名常量,可以使用元类.我在GitHub上使用元类提供了一个非常简单的解决方案,如果您希望值更多地提供有关其类型/名称的信息,这可能会有所帮助:
>>> from named_constants import Constants
>>> class Colors(Constants):
... black = 0
... red = 1
... white = 15
...
>>> c = Colors.black
>>> c == 0
True
>>> c
Colors.black
>>> c.name()
'black'
>>> Colors(0) is c
True
Run Code Online (Sandbox Code Playgroud)
这是稍微高级的Python,但仍然非常易于使用和方便.(该模块具有更多功能,包括常量只读,请参阅其自述文件.)
在各种存储库中都有类似的解决方案,但据我所知,它们要么缺少我期望从常量中获得的基本特征之一(如常量,或者是任意类型),要么它们具有深奥的功能.使它们不太普遍适用.但YMMV,我很感激反馈.:-)
dou*_*rve 17
编辑:添加了Python 3的示例代码
注意:这个其他答案看起来像是提供了一个更完整的实现,类似于以下(具有更多功能).
首先,制作一个元类:
class MetaConst(type):
def __getattr__(cls, key):
return cls[key]
def __setattr__(cls, key, value):
raise TypeError
Run Code Online (Sandbox Code Playgroud)
这可以防止更改静态属性.然后创建另一个使用该元类的类:
class Const(object):
__metaclass__ = MetaConst
def __getattr__(self, name):
return self[name]
def __setattr__(self, name, value):
raise TypeError
Run Code Online (Sandbox Code Playgroud)
或者,如果您使用的是Python 3:
class Const(object, metaclass=MetaConst):
def __getattr__(self, name):
return self[name]
def __setattr__(self, name, value):
raise TypeError
Run Code Online (Sandbox Code Playgroud)
这应该可以防止实例道具被更改.要使用它,继承:
class MyConst(Const):
A = 1
B = 2
Run Code Online (Sandbox Code Playgroud)
现在直接或通过实例访问的道具应该是不变的:
MyConst.A
# 1
my_const = MyConst()
my_const.A
# 1
MyConst.A = 'changed'
# TypeError
my_const.A = 'changed'
# TypeError
Run Code Online (Sandbox Code Playgroud)
小智 16
属性是创建常量的一种方法.您可以通过声明getter属性来执行此操作,但忽略setter.例如:
class MyFinalProperty(object):
@property
def name(self):
return "John"
Run Code Online (Sandbox Code Playgroud)
您可以查看我编写的文章,以找到使用Python属性的更多方法.
小智 15
PEP 591具有“最终”限定符。强制执行取决于类型检查器。
所以你可以这样做:
MY_CONSTANT: Final = 12407
Run Code Online (Sandbox Code Playgroud)
注意: Final关键字仅适用于 Python 3.8 版本
jas*_*uuu 15
我使用冻结数据类声明常量值,如下所示:
from dataclasses import dataclass
@dataclass(frozen=True)
class _Const:
SOME_STRING = 'some_string'
SOME_INT = 5
Const = _Const()
# In another file import Const and try
print(Const.SOME_STRING) # ITS OK!
Const.SOME_INT = 6 # dataclasses.FrozenInstanceError: cannot assign to field 'SOME_INT'
Run Code Online (Sandbox Code Playgroud)
这是"常量"类的实现,它使用只读(常量)属性创建实例.例如,可以使用Nums.PI获取已初始化为的值3.14159,并Nums.PI = 22引发异常.
# ---------- Constants.py ----------
class Constants(object):
"""
Create objects with read-only (constant) attributes.
Example:
Nums = Constants(ONE=1, PI=3.14159, DefaultWidth=100.0)
print 10 + Nums.PI
print '----- Following line is deliberate ValueError -----'
Nums.PI = 22
"""
def __init__(self, *args, **kwargs):
self._d = dict(*args, **kwargs)
def __iter__(self):
return iter(self._d)
def __len__(self):
return len(self._d)
# NOTE: This is only called if self lacks the attribute.
# So it does not interfere with get of 'self._d', etc.
def __getattr__(self, name):
return self._d[name]
# ASSUMES '_..' attribute is OK to set. Need this to initialize 'self._d', etc.
#If use as keys, they won't be constant.
def __setattr__(self, name, value):
if (name[0] == '_'):
super(Constants, self).__setattr__(name, value)
else:
raise ValueError("setattr while locked", self)
if (__name__ == "__main__"):
# Usage example.
Nums = Constants(ONE=1, PI=3.14159, DefaultWidth=100.0)
print 10 + Nums.PI
print '----- Following line is deliberate ValueError -----'
Nums.PI = 22
Run Code Online (Sandbox Code Playgroud)
感谢@MikeGraham的FrozenDict,我将其作为起点.已更改,因此不是Nums['ONE']使用语法Nums.ONE.
感谢@ Raufio的回答,想要覆盖__ setattr __.
或者对于具有更多功能的实现,请参阅GitHub上的 @Hans_meine的 named_constants
您可以使用namedtuple作为变通方法来有效地创建一个常量,该常量与Java中的静态最终变量(Java"常量")的工作方式相同.随着变通办法,它有点优雅.(更优雅的方法是简单地改进Python语言 - 什么样的语言可以让你重新定义math.pi? - 但我离题了.)
(在我写这篇文章时,我意识到这个问题的另一个答案提到了namedtuple,但我将继续在这里,因为我将展示一种语法,与Java中你所期望的更为平行,因为不需要创建一个命名类型为namedtuple迫使你去做.)
按照你的例子,你会记得在Java中我们必须在某个类中定义常量; 因为你没有提到一个班级名称,让我们称之为Foo.这是Java类:
public class Foo {
public static final String CONST_NAME = "Name";
}
Run Code Online (Sandbox Code Playgroud)
这是等效的Python.
from collections import namedtuple
Foo = namedtuple('_Foo', 'CONST_NAME')('Name')
Run Code Online (Sandbox Code Playgroud)
我想在这里添加的关键点是你不需要一个单独的Foo类型(一个"匿名命名元组"会很好,即使这听起来像一个矛盾),所以我们命名我们的namedtuple _Foo所以希望它不会逃到导入模块.
这里的第二点是我们立即创建 nametuple的一个实例,调用它Foo; 没有必要在单独的步骤中执行此操作(除非您愿意).现在,您可以在Java中执行以下操作:
>>> Foo.CONST_NAME
'Name'
Run Code Online (Sandbox Code Playgroud)
但你无法分配给它:
>>> Foo.CONST_NAME = 'bar'
…
AttributeError: can't set attribute
Run Code Online (Sandbox Code Playgroud)
致谢:我以为我发明了一种命名方法,但后来我发现别人给出了类似的(虽然不那么紧凑)答案.然后我也注意到Python中的"命名元组"是什么?,它指出sys.version_info现在是一个命名元组,所以也许Python标准库早就提出了这个想法.
请注意,遗憾的是(这仍然是Python),您可以Foo完全删除整个作业:
>>> Foo = 'bar'
Run Code Online (Sandbox Code Playgroud)
(捂脸)
但至少我们阻止Foo.CONST_NAME价值被改变,这比没有好.祝好运.
我们可以创建一个描述符对象。
class Constant:
def __init__(self,value=None):
self.value = value
def __get__(self,instance,owner):
return self.value
def __set__(self,instance,value):
raise ValueError("You can't change a constant")
Run Code Online (Sandbox Code Playgroud)
1) 如果我们想在实例级别使用常量,那么:
class A:
NULL = Constant()
NUM = Constant(0xFF)
class B:
NAME = Constant('bar')
LISTA = Constant([0,1,'INFINITY'])
>>> obj=A()
>>> print(obj.NUM) #=> 255
>>> obj.NUM =100
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: You can't change a constant
Run Code Online (Sandbox Code Playgroud)
2)如果我们只想在类级别创建常量,我们可以使用一个元类作为我们的常量(我们的描述符对象)的容器;所有下降的类都将继承我们的常量(我们的描述符对象),没有任何可以修改的风险。
# metaclass of my class Foo
class FooMeta(type): pass
# class Foo
class Foo(metaclass=FooMeta): pass
# I create constants in my metaclass
FooMeta.NUM = Constant(0xff)
FooMeta.NAME = Constant('FOO')
>>> Foo.NUM #=> 255
>>> Foo.NAME #=> 'FOO'
>>> Foo.NUM = 0 #=> ValueError: You can't change a constant
Run Code Online (Sandbox Code Playgroud)
如果我创建 Foo 的子类,这个类将继承常量而不能修改它们
class Bar(Foo): pass
>>> Bar.NUM #=> 255
>>> Bar.NUM = 0 #=> ValueError: You can't change a constant
Run Code Online (Sandbox Code Playgroud)
声明“常量”的 Pythonic 方式基本上是一个模块级变量:
RED = 1
GREEN = 2
BLUE = 3
Run Code Online (Sandbox Code Playgroud)
然后编写您的类或函数。由于常量几乎总是整数,并且它们在 Python 中也是不可变的,因此您几乎没有机会改变它。
当然,除非您明确设置RED = 2.
我会创建一个类来覆盖__setattr__基础对象类的方法并用它包装我的常量,请注意我使用的是python 2.7:
class const(object):
def __init__(self, val):
super(const, self).__setattr__("value", val)
def __setattr__(self, name, val):
raise ValueError("Trying to change a constant value", self)
Run Code Online (Sandbox Code Playgroud)
要包装一个字符串:
>>> constObj = const("Try to change me")
>>> constObj.value
'Try to change me'
>>> constObj.value = "Changed"
Traceback (most recent call last):
...
ValueError: Trying to change a constant value
>>> constObj2 = const(" or not")
>>> mutableObj = constObj.value + constObj2.value
>>> mutableObj #just a string
'Try to change me or not'
Run Code Online (Sandbox Code Playgroud)
它非常简单,但是如果你想使用与非常量对象相同的常量(不使用constObj.value),它会更加密集.这可能会导致问题,因此最好保持.value显示并知道您正在使用常量进行操作(可能不是最"pythonic"方式).
元组在技术上有资格作为常量,因为如果您尝试更改其中一个值,元组将引发错误.如果你想用一个值声明一个元组,那么在它唯一的值之后放一个逗号,如下所示:
my_tuple = (0 """Or any other value""",)
Run Code Online (Sandbox Code Playgroud)
要检查此变量的值,请使用与此类似的内容:
if my_tuple[0] == 0:
#Code goes here
Run Code Online (Sandbox Code Playgroud)
如果您尝试更改此值,则会引发错误.
不幸的是,Python还没有常量,这很遗憾.ES6已经为JavaScript添加了支持常量(https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/const),因为它在任何编程语言中都是非常有用的东西.正如在Python社区的其他答案中所回答的那样,使用约定 - 用户大写变量作为常量,但它不能防止代码中的任意错误.如果您愿意,可能会发现下一个单文件解决方案很有用(请参阅docstrings如何使用它).
文件constants.py
import collections
__all__ = ('const', )
class Constant(object):
"""
Implementation strict constants in Python 3.
A constant can be set up, but can not be changed or deleted.
Value of constant may any immutable type, as well as list or set.
Besides if value of a constant is list or set, it will be converted in an immutable type as next:
list -> tuple
set -> frozenset
Dict as value of a constant has no support.
>>> const = Constant()
>>> del const.temp
Traceback (most recent call last):
NameError: name 'temp' is not defined
>>> const.temp = 1
>>> const.temp = 88
Traceback (most recent call last):
...
TypeError: Constanst can not be changed
>>> del const.temp
Traceback (most recent call last):
...
TypeError: Constanst can not be deleted
>>> const.I = ['a', 1, 1.2]
>>> print(const.I)
('a', 1, 1.2)
>>> const.F = {1.2}
>>> print(const.F)
frozenset([1.2])
>>> const.D = dict()
Traceback (most recent call last):
...
TypeError: dict can not be used as constant
>>> del const.UNDEFINED
Traceback (most recent call last):
...
NameError: name 'UNDEFINED' is not defined
>>> const()
{'I': ('a', 1, 1.2), 'temp': 1, 'F': frozenset([1.2])}
"""
def __setattr__(self, name, value):
"""Declaration a constant with value. If mutable - it will be converted to immutable, if possible.
If the constant already exists, then made prevent againt change it."""
if name in self.__dict__:
raise TypeError('Constanst can not be changed')
if not isinstance(value, collections.Hashable):
if isinstance(value, list):
value = tuple(value)
elif isinstance(value, set):
value = frozenset(value)
elif isinstance(value, dict):
raise TypeError('dict can not be used as constant')
else:
raise ValueError('Muttable or custom type is not supported')
self.__dict__[name] = value
def __delattr__(self, name):
"""Deny against deleting a declared constant."""
if name in self.__dict__:
raise TypeError('Constanst can not be deleted')
raise NameError("name '%s' is not defined" % name)
def __call__(self):
"""Return all constans."""
return self.__dict__
const = Constant()
if __name__ == '__main__':
import doctest
doctest.testmod()
Run Code Online (Sandbox Code Playgroud)
如果这还不够,请参阅完整的测试用例.
import decimal
import uuid
import datetime
import unittest
from ..constants import Constant
class TestConstant(unittest.TestCase):
"""
Test for implementation constants in the Python
"""
def setUp(self):
self.const = Constant()
def tearDown(self):
del self.const
def test_create_constant_with_different_variants_of_name(self):
self.const.CONSTANT = 1
self.assertEqual(self.const.CONSTANT, 1)
self.const.Constant = 2
self.assertEqual(self.const.Constant, 2)
self.const.ConStAnT = 3
self.assertEqual(self.const.ConStAnT, 3)
self.const.constant = 4
self.assertEqual(self.const.constant, 4)
self.const.co_ns_ta_nt = 5
self.assertEqual(self.const.co_ns_ta_nt, 5)
self.const.constant1111 = 6
self.assertEqual(self.const.constant1111, 6)
def test_create_and_change_integer_constant(self):
self.const.INT = 1234
self.assertEqual(self.const.INT, 1234)
with self.assertRaisesRegexp(TypeError, 'Constanst can not be changed'):
self.const.INT = .211
def test_create_and_change_float_constant(self):
self.const.FLOAT = .1234
self.assertEqual(self.const.FLOAT, .1234)
with self.assertRaisesRegexp(TypeError, 'Constanst can not be changed'):
self.const.FLOAT = .211
def test_create_and_change_list_constant_but_saved_as_tuple(self):
self.const.LIST = [1, .2, None, True, datetime.date.today(), [], {}]
self.assertEqual(self.const.LIST, (1, .2, None, True, datetime.date.today(), [], {}))
self.assertTrue(isinstance(self.const.LIST, tuple))
with self.assertRaisesRegexp(TypeError, 'Constanst can not be changed'):
self.const.LIST = .211
def test_create_and_change_none_constant(self):
self.const.NONE = None
self.assertEqual(self.const.NONE, None)
with self.assertRaisesRegexp(TypeError, 'Constanst can not be changed'):
self.const.NONE = .211
def test_create_and_change_boolean_constant(self):
self.const.BOOLEAN = True
self.assertEqual(self.const.BOOLEAN, True)
with self.assertRaisesRegexp(TypeError, 'Constanst can not be changed'):
self.const.BOOLEAN = False
def test_create_and_change_string_constant(self):
self.const.STRING = "Text"
self.assertEqual(self.const.STRING, "Text")
with self.assertRaisesRegexp(TypeError, 'Constanst can not be changed'):
self.const.STRING += '...'
with self.assertRaisesRegexp(TypeError, 'Constanst can not be changed'):
self.const.STRING = 'TEst1'
def test_create_dict_constant(self):
with self.assertRaisesRegexp(TypeError, 'dict can not be used as constant'):
self.const.DICT = {}
def test_create_and_change_tuple_constant(self):
self.const.TUPLE = (1, .2, None, True, datetime.date.today(), [], {})
self.assertEqual(self.const.TUPLE, (1, .2, None, True, datetime.date.today(), [], {}))
with self.assertRaisesRegexp(TypeError, 'Constanst can not be changed'):
self.const.TUPLE = 'TEst1'
def test_create_and_change_set_constant(self):
self.const.SET = {1, .2, None, True, datetime.date.today()}
self.assertEqual(self.const.SET, {1, .2, None, True, datetime.date.today()})
self.assertTrue(isinstance(self.const.SET, frozenset))
with self.assertRaisesRegexp(TypeError, 'Constanst can not be changed'):
self.const.SET = 3212
def test_create_and_change_frozenset_constant(self):
self.const.FROZENSET = frozenset({1, .2, None, True, datetime.date.today()})
self.assertEqual(self.const.FROZENSET, frozenset({1, .2, None, True, datetime.date.today()}))
with self.assertRaisesRegexp(TypeError, 'Constanst can not be changed'):
self.const.FROZENSET = True
def test_create_and_change_date_constant(self):
self.const.DATE = datetime.date(1111, 11, 11)
self.assertEqual(self.const.DATE, datetime.date(1111, 11, 11))
with self.assertRaisesRegexp(TypeError, 'Constanst can not be changed'):
self.const.DATE = True
def test_create_and_change_datetime_constant(self):
self.const.DATETIME = datetime.datetime(2000, 10, 10, 10, 10)
self.assertEqual(self.const.DATETIME, datetime.datetime(2000, 10, 10, 10, 10))
with self.assertRaisesRegexp(TypeError, 'Constanst can not be changed'):
self.const.DATETIME = None
def test_create_and_change_decimal_constant(self):
self.const.DECIMAL = decimal.Decimal(13123.12312312321)
self.assertEqual(self.const.DECIMAL, decimal.Decimal(13123.12312312321))
with self.assertRaisesRegexp(TypeError, 'Constanst can not be changed'):
self.const.DECIMAL = None
def test_create_and_change_timedelta_constant(self):
self.const.TIMEDELTA = datetime.timedelta(days=45)
self.assertEqual(self.const.TIMEDELTA, datetime.timedelta(days=45))
with self.assertRaisesRegexp(TypeError, 'Constanst can not be changed'):
self.const.TIMEDELTA = 1
def test_create_and_change_uuid_constant(self):
value = uuid.uuid4()
self.const.UUID = value
self.assertEqual(self.const.UUID, value)
with self.assertRaisesRegexp(TypeError, 'Constanst can not be changed'):
self.const.UUID = []
def test_try_delete_defined_const(self):
self.const.VERSION = '0.0.1'
with self.assertRaisesRegexp(TypeError, 'Constanst can not be deleted'):
del self.const.VERSION
def test_try_delete_undefined_const(self):
with self.assertRaisesRegexp(NameError, "name 'UNDEFINED' is not defined"):
del self.const.UNDEFINED
def test_get_all_defined_constants(self):
self.assertDictEqual(self.const(), {})
self.const.A = 1
self.assertDictEqual(self.const(), {'A': 1})
self.const.B = "Text"
self.assertDictEqual(self.const(), {'A': 1, 'B': "Text"})
Run Code Online (Sandbox Code Playgroud)
优点:1.访问整个项目的所有常量2.严格控制常量值
缺点:1.不支持自定义类型和'dict'类型
笔记:
使用Python3.4和Python3.5进行测试(我使用'tox')
测试环境:
.
$ uname -a
Linux wlysenko-Aspire 3.13.0-37-generic #64-Ubuntu SMP Mon Sep 22 21:28:38 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
Run Code Online (Sandbox Code Playgroud)
from enum import Enum
class StringConsts(str,Enum):
ONE='one'
TWO='two'
print(f'Truth is {StringConsts.ONE=="one"}') #Truth is True
StringConsts.ONE="one" #Error: Cannot reassign
Run Code Online (Sandbox Code Playgroud)
Enum 和 str 的这种混合使您无需重新实现 setattr(通过 Enum)和与其他 str 对象(通过 str)进行比较。
这可能会完全弃用http://code.activestate.com/recipes/65207-constants-in-python/?in=user-97991。
如果您想要常量并且不关心它们的值,这里有一个技巧:
只需定义空类即可。
例如:
class RED:
pass
class BLUE:
pass
Run Code Online (Sandbox Code Playgroud)
小智 5
没有完美的方法可以做到这一点。据我了解,大多数程序员只会将标识符大写,因此 PI = 3.142 可以很容易地理解为一个常量。
另一方面,如果你想要一些实际上像常数一样的东西,我不确定你会找到它。无论你做什么,总会有某种方法来编辑“常量”,所以它实际上不会是一个常量。这是一个非常简单、肮脏的例子:
def define(name, value):
if (name + str(id(name))) not in globals():
globals()[name + str(id(name))] = value
def constant(name):
return globals()[name + str(id(name))]
define("PI",3.142)
print(constant("PI"))
Run Code Online (Sandbox Code Playgroud)
这看起来会生成一个 PHP 风格的常量。
事实上,某人改变价值所需要的只是这样:
globals()["PI"+str(id("PI"))] = 3.1415
Run Code Online (Sandbox Code Playgroud)
这对于您在这里找到的所有其他解决方案都是相同的 - 即使是创建类并重新定义设置属性方法的聪明解决方案 - 总会有办法解决它们。Python 就是这样。
我的建议是避免所有麻烦并大写您的标识符。它实际上并不是一个适当的常数,但话又说回来,什么都不是。
使用 namedtuple 有一种更简洁的方法:
from collections import namedtuple
def make_consts(name, **kwargs):
return namedtuple(name, kwargs.keys())(**kwargs)
Run Code Online (Sandbox Code Playgroud)
使用示例
CONSTS = make_consts("baz1",
foo=1,
bar=2)
Run Code Online (Sandbox Code Playgroud)
使用这种方法,您可以命名常量。
这是我创建的一组习语,试图改进一些已经可用的答案。
我知道使用常量不是pythonic,你不应该在家里这样做!
然而,Python 就是这样一种动态语言!这个论坛展示了如何创建看起来和感觉像常量的结构。这个答案的主要目的是探索语言可以表达什么。
请不要对我太苛刻:-)。
在这篇文章中,我将调用一个常量变量来引用值(不可变或其他)的常量。此外,我说一个变量在引用客户端代码无法更新其值的可变对象时具有冻结值。
这个习语创建了一个看起来像常量变量(又名 SpaceConstants)的命名空间。它是Alex Martelli对代码片段的修改,以避免使用模块对象。特别是,这个修改使用了我所说的类工厂,因为在SpaceConstants函数中,定义了一个名为SpaceConstants的类,并返回了它的一个实例。
我在stackoverflow和博客文章中探索了使用类工厂在 Python 中实现基于策略的设计相似。
def SpaceConstants():
def setattr(self, name, value):
if hasattr(self, name):
raise AttributeError(
"Cannot reassign members"
)
self.__dict__[name] = value
cls = type('SpaceConstants', (), {
'__setattr__': setattr
})
return cls()
sc = SpaceConstants()
print(sc.x) # raise "AttributeError: 'SpaceConstants' object has no attribute 'x'"
sc.x = 2 # bind attribute x
print(sc.x) # print "2"
sc.x = 3 # raise "AttributeError: Cannot reassign members"
sc.y = {'name': 'y', 'value': 2} # bind attribute y
print(sc.y) # print "{'name': 'y', 'value': 2}"
sc.y['name'] = 'yprime' # mutable object can be changed
print(sc.y) # print "{'name': 'yprime', 'value': 2}"
sc.y = {} # raise "AttributeError: Cannot reassign members"
Run Code Online (Sandbox Code Playgroud)
下一个习惯用法是对SpaceConstants的修改,其中引用的可变对象被冻结。这个实现利用了我所说的setattr和getattr函数之间的共享闭包。可变对象的值由函数共享闭包内部的变量缓存定义复制和引用。它形成了我所说的可变对象的闭包保护副本。
使用这个习惯用法时必须小心,因为getattr通过执行深度复制返回缓存的值。此操作可能会对大型对象产生显着的性能影响!
from copy import deepcopy
def SpaceFrozenValues():
cache = {}
def setattr(self, name, value):
nonlocal cache
if name in cache:
raise AttributeError(
"Cannot reassign members"
)
cache[name] = deepcopy(value)
def getattr(self, name):
nonlocal cache
if name not in cache:
raise AttributeError(
"Object has no attribute '{}'".format(name)
)
return deepcopy(cache[name])
cls = type('SpaceFrozenValues', (),{
'__getattr__': getattr,
'__setattr__': setattr
})
return cls()
fv = SpaceFrozenValues()
print(fv.x) # AttributeError: Object has no attribute 'x'
fv.x = 2 # bind attribute x
print(fv.x) # print "2"
fv.x = 3 # raise "AttributeError: Cannot reassign members"
fv.y = {'name': 'y', 'value': 2} # bind attribute y
print(fv.y) # print "{'name': 'y', 'value': 2}"
fv.y['name'] = 'yprime' # you can try to change mutable objects
print(fv.y) # print "{'name': 'y', 'value': 2}"
fv.y = {} # raise "AttributeError: Cannot reassign members"
Run Code Online (Sandbox Code Playgroud)
这个习惯用法是常量变量或ConstantSpace的不可变命名空间。它结合了非常简单的 Jon Betts 在stackoverflow 中的答案和class factory。
def ConstantSpace(**args):
args['__slots__'] = ()
cls = type('ConstantSpace', (), args)
return cls()
cs = ConstantSpace(
x = 2,
y = {'name': 'y', 'value': 2}
)
print(cs.x) # print "2"
cs.x = 3 # raise "AttributeError: 'ConstantSpace' object attribute 'x' is read-only"
print(cs.y) # print "{'name': 'y', 'value': 2}"
cs.y['name'] = 'yprime' # mutable object can be changed
print(cs.y) # print "{'name': 'yprime', 'value': 2}"
cs.y = {} # raise "AttributeError: 'ConstantSpace' object attribute 'x' is read-only"
cs.z = 3 # raise "AttributeError: 'ConstantSpace' object has no attribute 'z'"
Run Code Online (Sandbox Code Playgroud)
这个习惯用法是冻结变量或FrozenSpace的不可变命名空间。它是从前面的模式派生出来的,通过关闭生成的FrozenSpace类使每个变量成为受保护的属性。
from copy import deepcopy
def FreezeProperty(value):
cache = deepcopy(value)
return property(
lambda self: deepcopy(cache)
)
def FrozenSpace(**args):
args = {k: FreezeProperty(v) for k, v in args.items()}
args['__slots__'] = ()
cls = type('FrozenSpace', (), args)
return cls()
fs = FrozenSpace(
x = 2,
y = {'name': 'y', 'value': 2}
)
print(fs.x) # print "2"
fs.x = 3 # raise "AttributeError: 'FrozenSpace' object attribute 'x' is read-only"
print(fs.y) # print "{'name': 'y', 'value': 2}"
fs.y['name'] = 'yprime' # try to change mutable object
print(fs.y) # print "{'name': 'y', 'value': 2}"
fs.y = {} # raise "AttributeError: 'FrozenSpace' object attribute 'x' is read-only"
fs.z = 3 # raise "AttributeError: 'FrozenSpace' object has no attribute 'z'"
Run Code Online (Sandbox Code Playgroud)
小智 5
我正在尝试不同的方法来在 Python 中创建一个真正的常量,也许我找到了很好的解决方案。
例子:
为常量创建容器
>>> DAYS = Constants(
... MON=0,
... TUE=1,
... WED=2,
... THU=3,
... FRI=4,
... SAT=5,
... SUN=6
... )
Run Code Online (Sandbox Code Playgroud)
从容器中获取价值
>>> DAYS.MON
0
>>> DAYS['MON']
0
Run Code Online (Sandbox Code Playgroud)
用纯Python数据结构表示
>>> list(DAYS)
['WED', 'SUN', 'FRI', 'THU', 'MON', 'TUE', 'SAT']
>>> dict(DAYS)
{'WED': 2, 'SUN': 6, 'FRI': 4, 'THU': 3, 'MON': 0, 'TUE': 1, 'SAT': 5}
Run Code Online (Sandbox Code Playgroud)
所有常量都是不可变的
>>> DAYS.MON = 7
...
AttributeError: Immutable attribute
>>> del DAYS.MON
...
AttributeError: Immutable attribute
Run Code Online (Sandbox Code Playgroud)
仅对常量自动完成
>>> dir(DAYS)
['FRI', 'MON', 'SAT', 'SUN', 'THU', 'TUE', 'WED']
Run Code Online (Sandbox Code Playgroud)
排序就像list.sort
>>> DAYS.sort(key=lambda (k, v): v, reverse=True)
>>> list(DAYS)
['SUN', 'SAT', 'FRI', 'THU', 'WED', 'TUE', 'MON']
Run Code Online (Sandbox Code Playgroud)
python2与和 的兼容性python3
from collections import OrderedDict
from copy import deepcopy
class Constants(object):
"""Container of constant"""
__slots__ = ('__dict__')
def __init__(self, **kwargs):
if list(filter(lambda x: not x.isupper(), kwargs)):
raise AttributeError('Constant name should be uppercase.')
super(Constants, self).__setattr__(
'__dict__',
OrderedDict(map(lambda x: (x[0], deepcopy(x[1])), kwargs.items()))
)
def sort(self, key=None, reverse=False):
super(Constants, self).__setattr__(
'__dict__',
OrderedDict(sorted(self.__dict__.items(), key=key, reverse=reverse))
)
def __getitem__(self, name):
return self.__dict__[name]
def __len__(self):
return len(self.__dict__)
def __iter__(self):
for name in self.__dict__:
yield name
def keys(self):
return list(self)
def __str__(self):
return str(list(self))
def __repr__(self):
return '<%s: %s>' % (self.__class__.__name__, str(self.__dict__))
def __dir__(self):
return list(self)
def __setattr__(self, name, value):
raise AttributeError("Immutable attribute")
def __delattr__(*_):
raise AttributeError("Immutable attribute")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
907813 次 |
| 最近记录: |