Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Scoping issue with import

Reply
Thread Tools

Scoping issue with import

 
 
James Stroud
Guest
Posts: n/a
 
      02-28-2005
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
 
Reply With Quote
 
 
 
 
Carl Banks
Guest
Posts: n/a
 
      03-01-2005

James Stroud wrote:
> 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).


--
CARL BANKS

 
Reply With Quote
 
 
 
 
Steve Holden
Guest
Posts: n/a
 
      03-01-2005
James Stroud wrote:
> 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:

>>> def caller(f, arg):

... return f(arg)
...
>>> def call(summat):

... print summat * 3
...
>>> caller(call, 21)

63
>>> caller(call, "string")

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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Class-level variables - a scoping issue John Nagle Python 22 10-15-2010 11:50 PM
Import, Inheritance, Scoping -- I'm doing something wrong with one ofthese Elijah Newren Python 1 03-07-2009 05:23 PM
STL Map Scoping Issue sfncook@gmail.com C++ 12 08-15-2007 11:33 AM
import vs from module import : any performance issue? Pierre Rouleau Python 4 03-07-2004 05:06 PM
scoping issue Frederick Grim C++ 4 09-14-2003 07:39 AM



Advertisments