您好,我正在尝试使用从Creating constant in Python(在链接的第一个答案中)找到的示例在python中创建一个const,并使用instance作为模块。在
第一个文件常数py有
# Put in const.py...:
class _const:
class ConstError(TypeError): pass
def __setattr__(self,name,value):
if self.__dict__ in (name):
raise self.ConstError("Can't rebind const(%s)"%name)
self.__dict__[name]=value
import sys
sys.modules[__name__]=_const()
剩下的去测试.py例如。在
^{pr2}$虽然我已经做了2个更改,因为我使用的是python3,但仍然有错误
Traceback (most recent call last):
File "E:\Const_in_python\test.py", line 4, in <module>
const.magic = 23
File "E:\Const_in_python\const.py", line 5, in __setattr__
if self.__dict__ in (name):
TypeError: 'in <string>' requires string as left operand, not dict
我不明白第5行的错误是什么。有人能解释吗?纠正这个例子也不错。提前谢谢。在
这看起来很奇怪(它是从哪里来的?)在
不是吗
^{pr2}$这修正了你的例子
你真的需要这个警员黑客吗?很多Python代码似乎在某种程度上没有它也能工作
也许{a1}就是你要搜索的。在
支持str、int、float、datetime const字段实例将保持其基类型行为。 和orm模型定义一样,BaseConst是管理常量字段的常量助手。在
例如:
更多详细用法您可以阅读pypi url: pypi 或 github
同样的答案: Creating constant in Python
这条线:
应该是
^{pr2}$。。。您想知道属性是否在dict中,而不是dict是否在属性名中(这不起作用,因为字符串包含字符串,而不是字典)。在
相关问题 更多 >
编程相关推荐