简体   繁体   English

Python:从其他包中的文件访问静态类变量

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

I have the following file structure in my python project: 我的python项目中具有以下文件结构:

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: 我正在从Main文件中设置Common类的静态变量:

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: Common类中设置:

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: 现在,从Main文件中实例化一个Logger对象:

from classes.Logger import Logger

# initiate logger
log = Logger()

The Logger object reads the log file path from the Common object which works fine: Logger对象从Common对象读取日志文件路径,该文件工作正常:

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: 现在出现问题了:从我的Main文件中实例化一个Dictionary对象:

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. 不知何故,当字典中的Logger尝试从Common类加载日志文件路径时,它为空。

Why is that so? 为什么会这样? Shouldn't that be the same Common class and therefore holding the same static information here? 那不应该是相同的Common类,因此在这里保存相同的静态信息吗? Am I missing something? 我想念什么吗? Do I do the imports in a wrong way? 我会以错误的方式导入吗?

I'm working with Python 2.6.5 我正在使用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. Python模块中的几乎所有内容都是动态执行的,包括importclass语句。

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. 当您导入Directory模块首次执行模块,这意味着import其中进口语句Logger被执行,后Logger是进口class Directory:执行该实例化一个Logger对象。 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. 此时,尚未调用Common.set_log_file_path() ,因此Logger对象采用执行class Common:时定义的默认值。

The ”solution” would be importing Common and set the default path before executing anything that actually uses that default path attribute: “解决方案”将是导入Common并设置默认路径,然后执行实际上使用该默认路径属性的任何操作:

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. 因此,这在生产代码中很少见到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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