繁体   English   中英

如何将一个模块变量传递给 python 中的另一个模块?

[英]How can i pass one module variable to another module in python?

我会尝试下面的代码,但代码。

第一个模块

一个.py

class A:
    def run:
        self.x=20

第二个模块

b.py

 from a import A
 class B:
     def run:
          c= a.A()
          c.run()
          Print c.x

但它给了我错误。

 typeError: __init__() takes exactly 2 arguments (1 given)

抛出此错误是因为在类 A 中您需要声明参数:

def run(self, x):

您还需要在 b.py 中使用以下参数传递这些参数:

c.run('x-goes-here')

正如 Daniel 在评论中提到的,这是相当简单的 python(虽然对初学者来说很困惑)。 我建议您尝试 Codecademy 网站上的 python 'classes' 部分。

一个.py

class A:
def run(self):
    self.x = 20

b.py

from a import A
class B:
    def run(self):
        c = A()
        c.run()
        print c.x

b = B()
b.run()

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM