Scoping issue with import

J

James Stroud

Say I have a module, we'll call it "my_imported_mod". It contains a function
in it that calls another function, "myfun". The "myfun" function is in the
module "my_main_mod", that imports "my_imported_mod".

The code of "my_main_mod" might look like this:
==============
from my_imported_mod import *

def myfun():
print "stuff"
==============

the code of "my_imported_mod" might look like this:
==============
def somefun():
myfun()
==============

When trying to execute the function somefun(), I get a
NameError: global name 'myfun' is not defined

How to rectify this with minimal code change? How to let imported modules know
about the namespace they are getting imported into? I do not want to
restructure my code right now.

Thanks in advance for help.

James
 
C

Carl Banks

James said:
Say I have a module, we'll call it "my_imported_mod". It contains a function
in it that calls another function, "myfun". The "myfun" function is in the
module "my_main_mod", that imports "my_imported_mod".
[snip]

How to rectify this with minimal code change? How to let imported modules know
about the namespace they are getting imported into? I do not want to
restructure my code right now.

Thanks in advance for help.


Change it so that my_imported_mod imports my_main_mod. If A depends on
B, then A should import B, not the other way around.

If there's some good reason why B imports A despite this (and there
could be--say if myfun is some kind of a callback), then you should
probably pass myfun to the function in the imported module as an
argument. If that's not practical, put myfun into a third module, and
have my_imported_mod import that.

If you want to live dangerously, you could do something like this from
my_main_mod:

import my_imported_mod
my_imported_mod.myfun = myfun

I don't recommend it, though, because my_imported_mod is no longer
self-contained (i.e., it's a module not modular).
 
S

Steve Holden

James said:
Say I have a module, we'll call it "my_imported_mod". It contains a function
in it that calls another function, "myfun". The "myfun" function is in the
module "my_main_mod", that imports "my_imported_mod".

The code of "my_main_mod" might look like this:
==============
from my_imported_mod import *

def myfun():
print "stuff"
==============

the code of "my_imported_mod" might look like this:
==============
def somefun():
myfun()
==============

When trying to execute the function somefun(), I get a
NameError: global name 'myfun' is not defined

How to rectify this with minimal code change? How to let imported modules know
about the namespace they are getting imported into? I do not want to
restructure my code right now.

Thanks in advance for help.

James

You have had some good advice about avoiding circular imports.

I just wanted you to consider the coupling of your module. If the called
function is calling a function inside the module that's calling it, this
is often a clue that the inner function should be passed as a parameter
to the first one.

Let me know if this isn't comprehensible and I'll give you an example,
but to show this being done inside a single module I present the code below:
... return f(arg)
... ... print summat * 3
... stringstringstring

This also demonstrates quite nicely how Python doesn't choose to
discriminate between integers and strings at compile time, applying the
correct definition of multiplication when the operation actually has to
be performed.

Some people hate this, most people who read comp.lang.python regularly
will be quite happy to explain why it's a Good Thing (tm).

regards
Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top