Should certain "constants" be declared at the module level?

In PEP 8, he stated that "Constants are usually defined at the module level [...]." This makes sense for a standard library where constants tend to apply to the entire module, rather than to a specific class (e.g., zlib.MAX_WBITSor re.UNICODE). I am currently writing a module where the constants are associated with individual classes.

The module is designed to allow Python programs to work with an application-specific serialization format in which data blocks are grouped into “chunks”, and these fragments are further sorted into “regions”. The sizes of chunks and regions are useful constants for expansion, and I did this as class properties until I went through this line in PEP 8.

I tend to leave them as they are (PEP 8 also says that “stupid sequence is the hobgoblin of little minds,” after all), but I want to make sure that I don’t violate user expectations too much. (The module has not yet been released, so backward compatibility is not a problem.)

For reference, the style is "PEP 8" ...

CHUNK_SIZE_X = 16
CHUNK_SIZE_Z = 16
REGION_SIZE_X = 32
REGION_SIZE_Z = 32

def Chunk(object):
    # magic happens here

def Region(object):
    # magic happens here

... and my current "class-based" style ...

def Chunk(object):
    SIZE_X = 16
    SIZE_Z = 16

    # magic happens here

def Region(object):
    SIZE_X = 32
    SIZE_Z = 32

    # magic happens here
+5
2

, . . , PEP8 . : , .

Hungrarian_prefix_notation . , .

+7

? : " , ". .

0

All Articles