<bdo id='cKMUg'></bdo><ul id='cKMUg'></ul>
    <i id='cKMUg'><tr id='cKMUg'><dt id='cKMUg'><q id='cKMUg'><span id='cKMUg'><b id='cKMUg'><form id='cKMUg'><ins id='cKMUg'></ins><ul id='cKMUg'></ul><sub id='cKMUg'></sub></form><legend id='cKMUg'></legend><bdo id='cKMUg'><pre id='cKMUg'><center id='cKMUg'></center></pre></bdo></b><th id='cKMUg'></th></span></q></dt></tr></i><div id='cKMUg'><tfoot id='cKMUg'></tfoot><dl id='cKMUg'><fieldset id='cKMUg'></fieldset></dl></div>
  1. <tfoot id='cKMUg'></tfoot>

    <small id='cKMUg'></small><noframes id='cKMUg'>

  2. <legend id='cKMUg'><style id='cKMUg'><dir id='cKMUg'><q id='cKMUg'></q></dir></style></legend>

      确保只运行类的一个实例

      Ensure that only one instance of a class gets run(确保只运行类的一个实例)
      <legend id='5tGlr'><style id='5tGlr'><dir id='5tGlr'><q id='5tGlr'></q></dir></style></legend>

        <tfoot id='5tGlr'></tfoot>

      1. <small id='5tGlr'></small><noframes id='5tGlr'>

            <tbody id='5tGlr'></tbody>
          • <bdo id='5tGlr'></bdo><ul id='5tGlr'></ul>
            <i id='5tGlr'><tr id='5tGlr'><dt id='5tGlr'><q id='5tGlr'><span id='5tGlr'><b id='5tGlr'><form id='5tGlr'><ins id='5tGlr'></ins><ul id='5tGlr'></ul><sub id='5tGlr'></sub></form><legend id='5tGlr'></legend><bdo id='5tGlr'><pre id='5tGlr'><center id='5tGlr'></center></pre></bdo></b><th id='5tGlr'></th></span></q></dt></tr></i><div id='5tGlr'><tfoot id='5tGlr'></tfoot><dl id='5tGlr'><fieldset id='5tGlr'></fieldset></dl></div>
              • 本文介绍了确保只运行类的一个实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一个底层类,我想将其放在一些代码中。我只希望对给定的应用程序实例化或启动一次,尽管它可能会被调用多次。下面代码的问题是LowClass被一遍又一遍地重新启动。我希望每次测试只启动一次。。

                import logging
                
                class LowClass:
                
                    active = False
                
                    def __init__(self):
                        self.log = logging.getLogger()
                        self.log.debug("Init %s" % self.__class__.__name__)
                        if self.active:
                            return
                        else:
                            self.active = True
                        self.log.debug("Now active!")
                
                class A:
                    def __init__(self):
                        self.log = logging.getLogger()
                        self.log.debug("Init %s" % self.__class__.__name__)
                        self.lowclass = LowClass()
                
                class B:
                    def __init__(self):
                        self.log = logging.getLogger()
                        self.log.debug("Init %s" % self.__class__.__name__)
                        self.lowclass = LowClass()
                
                class C:
                    def __init__(self):
                        self.log = logging.getLogger()
                        self.log.debug("Init %s" % self.__class__.__name__)
                        self.a = A()
                        self.b = B()
                
                
                class ATests(unittest.TestCase):
                    def setUp(self):
                        pass
                
                    def testOne(self):
                        a = A()
                        b = B()
                
                    def testTwo(self):
                        c = C()
                

                谢谢您指出我的问题!!

                推荐答案

                请参阅singleton in python。

                这篇关于确保只运行类的一个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)
                Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)
                Is there a way of group by month in Pandas starting at specific day number?( pandas 有从特定日期开始的按月分组的方式吗?)
                Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)
                Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)
                Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)

                    <tbody id='ZnLfQ'></tbody>
                1. <legend id='ZnLfQ'><style id='ZnLfQ'><dir id='ZnLfQ'><q id='ZnLfQ'></q></dir></style></legend>

                2. <tfoot id='ZnLfQ'></tfoot>

                  <small id='ZnLfQ'></small><noframes id='ZnLfQ'>

                      <i id='ZnLfQ'><tr id='ZnLfQ'><dt id='ZnLfQ'><q id='ZnLfQ'><span id='ZnLfQ'><b id='ZnLfQ'><form id='ZnLfQ'><ins id='ZnLfQ'></ins><ul id='ZnLfQ'></ul><sub id='ZnLfQ'></sub></form><legend id='ZnLfQ'></legend><bdo id='ZnLfQ'><pre id='ZnLfQ'><center id='ZnLfQ'></center></pre></bdo></b><th id='ZnLfQ'></th></span></q></dt></tr></i><div id='ZnLfQ'><tfoot id='ZnLfQ'></tfoot><dl id='ZnLfQ'><fieldset id='ZnLfQ'></fieldset></dl></div>

                      • <bdo id='ZnLfQ'></bdo><ul id='ZnLfQ'></ul>