【问题标题】:How to access a variable from a class of another module如何从另一个模块的类中访问变量
【发布时间】:2014-01-15 10:33:33
【问题描述】:

我是一个完全的 Python 初学者,我在一个文件 commandline_reader.py 的类中创建了一个变量,我想从另一个脚本访问它。我试图通过将变量设为全局变量来做到这一点,但这是行不通的。

myscript.py:

    from commandline_reader import Commandline_Reader
    reader = Commandline_Reader('--get_serial_number')
    reader.run()

    print output

commandline_reader.py:

    class Commandline_Reader:
        def __init__(self,argString=''):
            global output
            output = []

        def run(self):
            # do stuff
            a = 'somevariable'
            output.append(a) 

当我运行 myscript.py 时,我总是收到 NameError: name 'output' is not defined。我读过这是因为全局变量仅在模块中定义。如何正确访问脚本中的输出变量?

【问题讨论】:

    标签: python class global


    【解决方案1】:

    哎哟。发生面向对象编程的全部原因是避免使用全局变量。使它们成为实例变量,以便在类中的任何位置访问它们。

    class Commandline_Reader:
        def __init__(self,argString=''):
            self.output = []
    
        def run(self):
            # do stuff
            a = 'somevariable'
            self.output.append(a) #output is now part of the instance Commandline reader and can be accessed anywhere inside the class. 
    
    
     clr = Commandline_Reader(argstring='--get_serial_number')
     clr.run()
     print clr.output
     >>>['somevariable']
    

    【讨论】:

    • 有很多原因。但是我想不出为什么你会在一个类中需要全局变量。
    • 例如,您想在几个@staticmethods 之间共享数据,您需要在该类中添加一个全局变量,不是吗?
    【解决方案2】:

    output 设为实例属性:

    class Commandline_Reader:
        def __init__(self,argString=''):
            self.output = [] # note use of self here
        def run(self):
            # do stuff
            a = 'somevariable'
            self.output.append(a) # and here
    

    通过实例访问它:

    print reader.output
    

    【讨论】:

      【解决方案3】:

      也许class attribute 更适合你?

      class Commandline_Reader:
      
          output = []
      
          def run(self):
              # do stuff
              a = 'somevariable'
              self.output.append(a) 
      

      【讨论】:

        【解决方案4】:

        只需从 run() 方法返回值

        myscript.py:

        from commandline_reader import Commandline_Reader
        reader = Commandline_Reader('--get_serial_number')
        output = reader.run()
        
        print output
        

        commandline_reader.py:

        class Commandline_Reader:
            def __init__(self,argString=''):
                self.output = []
        
            def run(self):
                # do stuff
                a = 'somevariable'
                self.output.append(a)
                return self.output 
        

        【讨论】:

        • 你的意思是 self.output,现在的样子,init 中的 output = [] 什么都不做。 list.append 也返回 None,所以你不应该返回它。
        • 代码中的输出范围不知道没有实例属性self的init函数。这仍然会返回名称错误。
        • 谢谢,我希望有这样一个简单的解决方案!
        • @Mailerdaimon 您正在返回变量,但是如何跨模块全局访问变量。
        猜你喜欢
        • 2020-12-30
        • 2016-05-15
        • 1970-01-01
        • 2015-02-28
        • 2012-12-10
        • 2021-02-13
        • 2011-10-22
        • 2021-08-09
        • 1970-01-01
        相关资源
        最近更新 更多