如何从模块中的模块内部更改变量。。。(困惑)

2024-03-29 08:31:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我的主模块中有一个变量,它是使用另一个模块更改的,但是我想通过另一个模块更改主模块中的变量。我是编程新手,所以我真的不知道如何解释这些东西-抱歉,如果我问了一个愚蠢的问题。你知道吗

程序的层次结构有点像这样:

主要
---功能
---Pygame\u处理
----特点

我使用“Features”模块来更改“Main”中的变量。我只是通过从“Features”中获取定义的变量来实现这一点。但是当我通过“Pygame\u handling”更改变量时,它在“Main”模块中创建的“Features”对象中没有更改。你知道吗

你知道吗主.py你知道吗

import Features
class Simulator:
    def __init__(self):
        self.Features  = Features.Methods()
        self.variables = self.Features.dictionary

        self.PyObject  = Pygame_handling.Window()

皮加梅_处理.py你知道吗

import Features
class Window:
    def __init__(self):
        self.Features = Features.Methods()
        dict = {"some":"dict"}
        self.Features.monitor_changes(dict)

Tags: 模块pyimportselfinitmaindef编程
1条回答
网友
1楼 · 发布于 2024-03-29 08:31:15

如何初始化这些类? 通常当我需要做这样的事情时,我会这样编码:

py1.py:

class Test(object):
    def test_print(self):
        print 'Hi!'

TEST = Test()

py2.py:

from py1 import TEST
TEST.test_print()

# Adding new stuff to the TEST initialized class
TEST.new_var = 50
print TEST.new_var
#output: 50

现在您可以使用该模块上的初始化类。你知道吗

相关问题 更多 >