简体   繁体   English

python从不同的模块访问变量

[英]python accessing variable from different module

I have 2 scripts. 我有2个脚本。 Main.py and module1.py . Main.pymodule1.py

Module1.py Module1.py

class classA():
    def method1(self):
        self.c=a+b
        ....
        ....
    def method2():
        ....
class classB():
    ....
class classC():
    ....

Main.py 主程序

import module1
print module1.classA.c    

I am trying to access the variable c from module1.py classA in the main.py But when i run main.py, it gave me error saying "c is not defined".What is the correct way to do it? 我试图从访问变量c module1.py classAmain.py但是当我运行main.py,它给了我错误说“C没有定义”。什么是做了正确的方法是什么? i have tried _builtin_ as well but it gave me the same error. 我也尝试过_builtin_ ,但这给了我同样的错误。

You did not call the function, thus self.c would never have been created. 您没有调用该函数,因此将永远不会创建self.c Also, create an instance of the class: 另外,创建该类的实例:

import module1
myinst = module1.classA()
myinst.method1()
print myinst.c

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

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