accessing variable of the __main__ module

News123 news1234 at free.fr
Sat Mar 20 08:16:08 EDT 2010


Hi,


I wondered about the best way, that a  module's function could determine
the existance and value of variables in the __main__ module.


What I came up with is:
########### main.py ##########
import mod
A = 4
if __name__ == "__main__": mod.f()
########### mod.py ##########
def f():
    try:
        from __main__ import A
    except ImportError as e:
        A = "does not exist"
    print "__main__.A" ,A

Is there anything better / more pythonic?

Thanks in advance and bye


N



More information about the Python-list mailing list