定义常量变量python 3的最佳方法是什么

Tom*_*mE8 10 python constants configuration-files python-3.x

我在python中编写一个包含许多常量变量的程序.我想创建一个文件,它将保存所有这些变量,如C中包含许多#define的.h文件.我尝试使用configparser但是我发现使用它并不容易.

你知道更好的方法吗?

sch*_*tte 18

Python不允许像C或C++那样的常量声明.

通常在Python中,常量是大写的(PEP 8标准),这有助于程序员知道它是常量.

防爆. MY_CONSTANT = "Whatever"

另一种有效的方法,我不会使用,但听说过,使用的方法是:

def MY_CONSTANT():
    return "Whatever"
Run Code Online (Sandbox Code Playgroud)

从理论上讲,调用MY_CONSTANT()行为就像一个常量.

编辑

就像评论所说,有人可以通过致电改变价值

MY_CONSTANT = lambda: 'Something else'
Run Code Online (Sandbox Code Playgroud)

但是不要忘记同一个人可以MY_CONSTANT = "Something else"在第一个例子中调用并更改初始值.在这两种情况下都不太可能,但可能.

  • `MY_CONSTANT = lambda:'别的东西'`–不知道为什么要麻烦“常量函数”,它不再是常量。 (5认同)

Eve*_*awk 10

Python 3.8+ 中的常量(某种意义上)

Python 3.8 引入了typing.Final类型限定符,用于指示不应重新分配、重新定义或覆盖变量或属性。

PEP 591 -- 为打字添加最终限定符

from typing import Final

# Annotate module variables
# (with or without an explicit type, using the syntax Final[<type>])
# (type is auto-determined in absence of an explicit type)
PI: Final[float] = 3.141592654
ANSWER_TO_EVERYTHING: Final = 42


# Annotate instance variables in class bodies
# (explicit type is needed if no value is assigned)
class Point:
    x: Final[int]
    y: Final = 0

    def __init__(self, x: int):
        self.x = x


# Annotate instance variables directly
# (only allowed in __init__ methods)
class Person:
    def __init__(self, birth_year: int):
        self.birth_year: Final = birth_year
Run Code Online (Sandbox Code Playgroud)

如果您重新分配或重新定义Final变量,Linter 和类型检查器将向您显示警告。请注意,没有运行时检查,因此您仍然可以运行下面的代码。

ANSWER_TO_EVERYTHING: Final = 42
ANSWER_TO_EVERYTHING = 420  # shows warning
print(ANSWER_TO_EVERYTHING)  # prints 420
Run Code Online (Sandbox Code Playgroud)

还有typing.final装饰器,用于限制继承类和覆盖方法。


900*_*000 9

Python中没有常量,就像C或Java中的常量一样。您可以通过函数来​​模仿它们:

def FOO():
  return "foo"
Run Code Online (Sandbox Code Playgroud)

您可以将函数调用包装在一个属性中,从而使其看起来像一个变量:

class Const:
  @property
  def FOO(self):
    return "foo"

CONST = Const()  # You need an instance

if something == CONST.FOO:
  ...
Run Code Online (Sandbox Code Playgroud)

借助一些元数据,可以使用简洁的语法获取无法设置的属性:

def const(cls):
    # Replace a class's attributes with properties,
    # and itself with an instance of its doppelganger.
    is_special = lambda name: (name.startswith("__") and name.endswith("__"))
    class_contents = {n: getattr(cls, n) for n in vars(cls) if not is_special(n)}
    def unbind(value):  # Get the value out of the lexical closure.
        return lambda self: value
    propertified_contents = {name: property(unbind(value))
                             for (name, value) in class_contents.items()}
    receptor = type(cls.__name__, (object,), propertified_contents)
    return receptor()  # Replace with an instance, so properties work.


@const
class Paths(object):
    home = "/home"
    null = "/dev/null"
Run Code Online (Sandbox Code Playgroud)

现在,您可以Paths.home按常规值访问,但不能为其分配值。您可以定义多个用修饰的类@const,因为您可能会使用多个.h文件。


Art*_*epo 6

你可以使用这样的东西:

文件结构:

myapp/
    __init__.py
    settings.py
    main.py
Run Code Online (Sandbox Code Playgroud)

设置.py

CONST_A = 'A'
CONST_B = 'B'
Run Code Online (Sandbox Code Playgroud)

__init__.py

from . import settings as global_settings


class Settings:

    def __init__(self):
        for setting in dir(global_settings):
            if setting.isupper():
                setattr(self, setting, getattr(global_settings, setting))

    def __setattr__(self, attr, value):
        if not getattr(self, attr, None):
            super().__setattr__(attr, value)
        else:
            raise TypeError("'constant' does not support item assignment")


settings = Settings()
Run Code Online (Sandbox Code Playgroud)

主文件

import settings

print(settings.CONST_A)  # prints A

settings.CONST_A = 'C'  # raises TypeError error

print(settings.CONST_A)  # prints A

settings.CONST_C = 'C'  # also able to add new constants
print(settings.CONST_C)  # prints C
Run Code Online (Sandbox Code Playgroud)

Settings类中覆盖 __setattr__使所有属性都为只读。唯一的要求是将settings.py 中的所有常量都用大写字母书写。但请注意,如果您直接导入变量,它将无法正常工作:

from settings import CONST_A

print(settings.CONST_A)  # prints A

settings.CONST_A = 'C'  # sets C

print(settings.CONST_A)  # prints C
Run Code Online (Sandbox Code Playgroud)


sar*_*n3h 5

只需定义一个constants.py文件并写入所有常量即可。Python 中没有其他魔法。使用大写字母作为一般约定。