简体   繁体   English

在python中的模块之间传递对象的最合适方法是什么?

[英]What is the most appropriate way to pass objects between modules in python?

I have to access an object in a module from another module: 我必须从另一个模块访问模块中的对象:

module_1.py: module_1.py:

import module_2

class A():
    def __init__():
         do_sth()

class B():
    def __init__():
         do_sth()
         self.x = some_object()
         do_sth_else(x)

    def show():
        show_gui()

def start():
    y = B()
    y.show()

if __name__ == "__main__":
    start()

module_2.py: module_2.py:

def run_it(arg):
    run_run_run()

I need self.x object from module_1 so that I can pass it as an argument to run_it() function in module_2 . 我需要self.x从对象module_1这样我可以通过它作为参数传递给run_it()函数module_2 Note that module_1 also imports module_2 . 请注意, module_1也会导入module_2

Is there a conventional method to access objects in other modules? 是否有访问其他模块中对象的常规方法?

Now with your corrections, it looks like you want something like this: 现在,您进行了更正,看起来您想要这样的东西:

from module_1 import B

run_it(B().x)

Which raises a question: If x is the same for all objects of B , why not make it a class member instead of an instance member. 这就提出了一个问题:如果x对于B所有对象都是相同的,为什么不使它成为类成员而不是实例成员。 Something like: 就像是:

class B:
    x = some_object()

    def __init__(self):
         do_sth()
         do_sth_else(B.x)

    def show(self):
        show_gui()

And in the other module 在另一个模块中

run_it(B.x)

Addressing your comment: y is local to start . 解决您的评论: y是本地的start You will need to return it: 您将需要返回它:

def start():
    y = B()
    y.show()
    return y

And then 接着

run_it(start())

Also y.show() will fail horribly as show doesn't accept parameters and will get passed y as self . 另外y.show()会严重失败,因为show不接受参数,并且将y作为self传递。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在导入的代码库之间传递对象的Pythonic方法是什么? - What is the most Pythonic way to pass objects between imported libraries of code? 有没有办法在不同模块之间传递 Python 中的 Class 对象并保留命名空间? - Is there a way to pass Class objects in Python between different modules and retain namespace? 在python中导入模块的最pythonic方法是什么 - What is the most pythonic way to import modules in python 在Mac上安装python模块最兼容的方法是什么? - What is the most compatible way to install python modules on a Mac? 在Python中模块之间传递数据的奇怪方法:它是如何工作的? - Strange way to pass data between modules in Python: How does it work? 将嵌套的 Python 对象转换为字典的最经济的方法是什么? - What is the most economical way to convert nested Python objects to dictionaries? 在Python中将对象属性传递给函数的最优选方法是什么? - What is the most preferred way to pass object attributes to a function in Python? 2015年最适合使用Python的Neo4j的方法 - The most appropriate way to use Neo4j from Python in 2015 在没有日历模块的 python 中编程日志。 编程日期的最合适方法是什么,必须能够跳过空白页 - programming a journal in python without calender module. what is the most appropriate way to program dates, must be able to skip empty pages 检查特定的python模块并安装未找到的模块的最有效方法是什么? - What is the most efficient way of checking for specific python modules and installing the ones not found?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM