简体   繁体   中英

Python Module Level Constants

I am creating my first rather complex module, and like the wx module, I wish to provide a variety of constants to ease the programmers task of setting styles.

assuming the following directory structure:

Module
   +-__init__py
   +- Frame.py

and the contents of init .py

__ALL__=["Frame.py"]
FRAME_DEFAULT_SIZE = (640, 480)
FRAME_DEFAULT_TITLE = "Some Simple Title"

then in Frame.py

class Frame(object)
    """Some docstrings go here
    """
    def __init__(self, parent, ID=-1,title=HOW DO I REFERENCE MY CONSTANT,
                 size = HOW DO I REFERENCE THIS CONSTANT):
        etc ...

If I were to import my module for use in a piece of program code the constants would be available as Module.FRAME_DEFAULT_SIZE, and Module.FRAME_DEFAULT_TITLE respectively.

But how do i reference them as part of a class definition which is supposed to be in the modules name-space, when they are defined in a separate file?

I realize that constants (if named well and used well) should apply only to a parent class and it's children, so I could instantiate them in the Frame.py file. But how does one do it this way?

Any help appreciated here.

Import Module and access it off that. That part doesn't change just because you're in a submodule.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM