简体   繁体   中英

python accessing variable from different module

I have 2 scripts. Main.py and 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? i have tried _builtin_ as well but it gave me the same error.

You did not call the function, thus self.c would never have been created. Also, create an instance of the class:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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