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
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
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
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:
>> ###
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
* 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():
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: