【发布时间】:2010-09-23 07:46:29
【问题描述】:
我想在 Python 中为 2 个类使用一个常量。 哪个是更好的方法?提前致谢!
MY_COLOR = "#000001" # <-------- Are correct here?
BLACK = "#000000" # <-------- Are correct here?
class One:
MY_FONT = "monospace"
def __init__(self):
if MY_COLOR == BLACK:
print("It's black")
if self.MY_FONT == "monospace":
print("Font equal")
class Two:
def __init__(self):
if MY_COLOR == BLACK:
print("It's black")
【问题讨论】:
-
您不应该将“#111111”和“#000000”作为常量。
-
你应该查找“常量”的定义。请描述您的用例是什么,因为在上面的代码中使用常量没有意义。
-
我编辑了这个例子;)我询问了这个职位;)谢谢 pyfunc 和 AndiDog!