Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 像getLogger一样获取对同一对象的引用_Python_Python 2.7_Logging - Fatal编程技术网

Python 像getLogger一样获取对同一对象的引用

Python 像getLogger一样获取对同一对象的引用,python,python-2.7,logging,Python,Python 2.7,Logging,我正在用Python开发一个CLI工具。我有一个JSON的大结构文件,我通过DataStructure类解析它 完成后,它将在项目中的所有类之间传递该对象。这有点烦人,我想知道是否有人知道如何使用logging.getLogger()创建一个模块,例如名为data,并在其中定义一个返回全局变量的函数,就像logging库那样引用对象。例如,data.py: _impl = None def get(): if _impl is None: _impl = Somethi

我正在用Python开发一个CLI工具。我有一个JSON的大结构文件,我通过
DataStructure
类解析它


完成后,它将在项目中的所有类之间传递该对象。这有点烦人,我想知道是否有人知道如何使用
logging.getLogger()

创建一个模块,例如名为
data
,并在其中定义一个返回全局变量的函数,就像
logging
库那样引用对象。例如,
data.py

_impl = None

def get():
    if _impl is None:
        _impl = SomethingThatMakesTheData()
    return _impl

然后,您可以从任何需要的地方导入数据,并执行数据。get()。

只需创建一个名为
数据的模块,并在其中定义一个返回全局变量的函数。例如,
data.py

_impl = None

def get():
    if _impl is None:
        _impl = SomethingThatMakesTheData()
    return _impl
import json
class DataStructure:

    @classmethod
    def parsinglogic(cls):
        ##Define JSON parsing logic here
        data=json.loads('filename')
        return data
然后,您可以
导入数据
并从任何需要的地方执行
数据。get()

import json
class DataStructure:

    @classmethod
    def parsinglogic(cls):
        ##Define JSON parsing logic here
        data=json.loads('filename')
        return data
DataStructure.parsinglogic()
可用于调用JSON解析器。请参阅每次调用此解析器时,都会读取JSON文件

而是创建实例方法和实例变量来存储值并将其传递给其他类

import json
class DataStructure:
    def __init__(self,data=None):
        self.data=data

    def parsinglogic(self):
        ##Define JSON parsing logic here
        self.data=json.loads('filename')
        return self.data


d=DataStructure()
data=d.parsinglogic()
#Pass this data to the other classes
DataStructure.parsinglogic()
可用于调用JSON解析器。请参阅每次调用此解析器时,都会读取JSON文件

而是创建实例方法和实例变量来存储值并将其传递给其他类

import json
class DataStructure:
    def __init__(self,data=None):
        self.data=data

    def parsinglogic(self):
        ##Define JSON parsing logic here
        self.data=json.loads('filename')
        return self.data


d=DataStructure()
data=d.parsinglogic()
#Pass this data to the other classes

明亮的我执行了以下操作:从服务导入DataStructureHelper implDsh=None def getDsh():如果implDsh为None,则全局implDsh:implDsh=DataStructureHelper.DataStructureHelper()返回implDsh def setDsh():全局implDsh implDsh=DataStructureHelper.DataStructureHelper()返回implDshBrilliant。我执行了以下操作:从服务导入DataStructureHelper implDsh=None def getDsh():如果implDsh为None,则全局implDsh:implDsh=DataStructureHelper.DataStructureHelper()返回implDsh def setDsh():全局implDsh implDsh=DataStructureHelper.DataStructureHelper()返回implDsh