简体   繁体   中英

Python: Accessing a static class variable from a file in a different package

I have the following file structure in my python project:

Main.py
classes
  |--- __init__.py
  |--- Common.py
  |--- Logger.py
  |--- Dictionary.py

I'm setting a static variable of the Common class from within my Main file:

from classes.Common import Common

# set path to the log file for log output
Common.set_log_file_path(C:\\logging\\output.txt"))

which gets set in the Common class:

class Common():
    log_file_path = ''

    @staticmethod
    def set_log_file_path(log_file_path):
        Common.log_file_path = log_file_path

Now I instantiate a Logger object from within my Main file:

from classes.Logger import Logger

# initiate logger
log = Logger()

The Logger object reads the log file path from the Common object which works fine:

from Common import Common

class Logger():
    log_file_path = ''

    def __init__(self, log_file_path=''):
        # if not specified, take default log file path from Common class
        if log_file_path == '':
            self.log_file_path = Common.log_file_path

Now comes the problem: From my Main file I instantiate a Dictionary object:

from classes.Dictionary import Dictionary

# load dictionary
dictionary = Dictionary()

In the dictionary object I also want to have a logger, so I create one there:

from Logger import Logger

class Dictionary():
    log = Logger()

    def __init__(self):
        Dictionary.log.info('Dictionary > __init__()')

But this one doesn't work. Somehow when the Logger from within the Dictionary tries to load the log file path from the Common class, it is empty.

Why is that so? Shouldn't that be the same Common class and therefore holding the same static information here? Am I missing something? Do I do the imports in a wrong way?

I'm working with Python 2.6.5

EDIT: My imports are as follows:

Main.py imports Dictionary, Logger, Common
Dictionary.py imports Logger
Logger.py imports Common
Common has no imports

Almost everything in a Python module is dynamically executed — including import and class statements.

When you import the Directory module the first time the module is executed which means the import statement which imports Logger is executed and after the Logger is imported the class Directory: is executed which instantiates a Logger object. At this point your Common.set_log_file_path() was not yet called so that Logger object takes the default value which is defined when class Common: was executed.

The ”solution” would be importing Common and set the default path before executing anything that actually uses that default path attribute:

from classes.Common import Common
Common.log_file_path = 'C:/logging/output.txt'
from classes.Directory import Directory
from classes.Logger import Logger

Solution in quotes because having imports depending on other code being executed before is something that can turn into little nightmares very easily. Therefore it is something very seldom seen in productive code.

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