Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.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 我是否必须声明变量为全局变量才能在类之外使用它们?_Python_Python 3.x_Global Variables - Fatal编程技术网

Python 我是否必须声明变量为全局变量才能在类之外使用它们?

Python 我是否必须声明变量为全局变量才能在类之外使用它们?,python,python-3.x,global-variables,Python,Python 3.x,Global Variables,似乎这是我找到的唯一方法,可以只从另一个类中的class-def中提取变量,而不必在我想要调用变量的类中拥有所有的打印和函数。我是对的,还是有其他更简单的方法 class MessageType: def process_msg1(self,data) item1 = [] item2 = [] item3 = [] item1.append(float(stuff)*stuff) item2.append

似乎这是我找到的唯一方法,可以只从另一个类中的class-def中提取变量,而不必在我想要调用变量的类中拥有所有的打印和函数。我是对的,还是有其他更简单的方法

class MessageType:
    def process_msg1(self,data)
        item1 = []
        item2 = []
        item3 = []

        item1.append(float(stuff)*stuff)
        item2.append(float(stuff)*stuff)
        item2.append(float(stuff)*stuff)
        print('printing and plotting stuff')

        return(array(item1), array(item2), array(item3))

class PrintMSG(MessageType):
    def process_msg1(self, data):
        (i1, i2, i3) = messagetype.process_msg1(data)
        print('printing plus plotting using variables from class Message')

#processing piece
keep_asking = True
while keep_asking:
    command_type = input("What message type do you want to look at?")
    if command_type == 'msg type1':
        msg = MessageType()
        print_msg = PrintMSG()
        messagetype.process_msg1(self,data)
        print_msg.process_msg1(data)
    elif:
        print('other stuff')
    wannalook = input('Want to look at another message or no?')
    if not wannalook.startswith('y'):
        keep_asking = False
我遇到的问题是,我有一个项目,需要从其他类中的其他类调用变量。我还有几个带有全局变量的类,但我的问题是
(i1,i2,i3)=messagetype.process\u msg1(data)
再次运行整个class-def,而不仅仅是调用数组变量。如果在
#processing piece
下我已经调用了class-def,有没有方法调用它们


参考先前发布的问题

类应该是代码和数据的独立单元。拥有几个带有几个全局变量的类表明您几乎肯定做错了什么

持续时间应长于单个方法调用的类数据,无论是稍后在内部使用还是公开给其他代码,都应保存到
self
。因此,您的
item1
item2
item3
应该在
\uuuuuuuuuuu
方法中初始化,然后
process\u msg1
应该在不初始化的情况下更新它们。大概是这样的:

class MessageType:

    def __init__(self):
        self.item1 = []
        self.item2 = []
        self.item3 = []

    def process_msg1(self, data)
        self.item1.append(float(stuff) * stuff)
        self.item2.append(float(stuff) * stuff)
        self.item2.append(float(stuff) * stuff)
        print('printing and plotting stuff')
        return(array(self.item1), array(self.item2), array(self.item3))

然后,一旦您创建了一个实例(
message\u type=MessageType()
)并调用了
process\u msg1
方法(
message\u type.process\u msg1(1.0,2.3,3.14159)
),其他代码就可以作为
message\u type.item1
等)访问它。

我不知道您想问什么。代码示例会有所帮助。你为什么要从一个类中“提取”一些东西呢?请点击我问题中给出的链接…示例代码在那里..我试图使用一个类中的变量,这些变量有我在其他类中需要的计算。那里有大量的东西,你所指的完全不明确。请用一个自我解释的代码示例更新您的问题。抱歉…早上太早了。请把我的观点告诉我。在我看来,你根本没有正确使用OOP。变量可能会成为对象的属性,您可以随时“从外部”访问它们。“东西”实际上来自csv文件中的字段。我尝试使用messagetype.item1,但后来被告知item1未定义。
item1
未定义,因为您没有在它前面加上
self.
——这意味着它是一个局部变量,并且在
messagetype.process\u msg1
方法执行完毕后立即被销毁。