Re: accessing variable of the __main__ module

2010-03-22 Thread Jean-Michel Pichavant
News123 wrote: 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

Re: accessing variable of the __main__ module

2010-03-20 Thread Dave Angel
News123 wrote: Steven D'Aprano wrote: Now, in your case you escape that trap, because the import is inside a function, so it doesn't occur until you call the function. But it is still considered poor practice: it is best to avoid circular imports unless you really, really need them. T

Re: accessing variable of the __main__ module

2010-03-20 Thread Steven D'Aprano
On Sat, 20 Mar 2010 14:32:03 +0100, News123 wrote: >> You try to import from "__main__", but the other module is called >> "main". __main__ is a special name, which Python understands as meaning >> "this module that you are in now". For example: > > My choice of names was perhaps not very smart. I

Re: accessing variable of the __main__ module

2010-03-20 Thread News123
Hi Steven, Steven D'Aprano wrote: > On Sat, 20 Mar 2010 13:16:08 +0100, News123 wrote: > >> 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: >> ###

Re: accessing variable of the __main__ module

2010-03-20 Thread Steven D'Aprano
On Sat, 20 Mar 2010 13:16:08 +0100, News123 wrote: > 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

Re: accessing variable of the __main__ module

2010-03-20 Thread Alf P. Steinbach
* News123: 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():

accessing variable of the __main__ module

2010-03-20 Thread News123
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: