473,394 Members | 1,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Scoping issue with import

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
Jul 18 '05 #1
2 1126

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

Jul 18 '05 #2
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
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: It's me | last post by:
This must be another newbie gotchas. Consider the following silly code, let say I have the following in file1.py: #============= import file2 global myBaseClass myBaseClass =...
3
by: bens | last post by:
I'm trying to return an 'mmap' object from a function. The return works, but some of the object's methods crash. Here are two examples doesntwork.py --------------------------- import mmap...
2
by: Robert M. Gary | last post by:
I'm curious what the ANSI C++ standard says about nested classes. I'm not able to find where in the ANSI C++ standard this is addressed. The issue is the accessibility of sibling nested classes....
1
by: Michael | last post by:
I am having a problem with scoping of parameters in my XSLT Stylesheet...here is the stylesheet (the xml document is irrelevant for the example) <?xml version="1.0" encoding="UTF-8"?>...
7
by: Charles Krug | last post by:
List: I've a module that's not doing what I expect. My guess is that I don't quite understand the scoping rules the way I should. I have an object that's costly to create. My thought was to...
9
by: NevilleDNZ | last post by:
Can anyone explain why "begin B: 123" prints, but 456 doesn't? $ /usr/bin/python2.3 x1x2.py begin A: Pre B: 123 456 begin B: 123 Traceback (most recent call last): File "x1x2.py", line 13,...
3
by: morris.slutsky | last post by:
So every now and then I like to mess around with hobby projects - I often end up trying to write an OpenGL video game. My last attempt aborted due to the difficulty of automating game elements and...
17
by: Chad | last post by:
The following question stems from Static vs Dynamic scoping article in wikipedia. http://en.wikipedia.org/wiki/Scope_(programming)#Static_versus_dynamic_scoping Using this sites example, if I...
1
by: jholg | last post by:
Hi, regarding automatically adding functionality to a class (basically taken from the cookbook recipee) and Python's lexical nested scoping I have a question wrt this code: #-----------------...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.