473,395 Members | 1,571 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,395 software developers and data experts.

"from module import *" and modifying module's top-level vars

Hi everyone,

I define some vars and functions in a "support" module which gets
called from my
main app module. Using Python 2.5.

I import all symbols in the support module at the top of the main
module through:

from support import *

Is there a way for me to modify a top-level ("global"?) variable
defined in module
support from the code in the main module and still have those changes
visible to
code in the support module?

Consider the following example:

# support.py

globvar = "old"

def fun():
print "fun(): globvar==" + globvar

# main.py

from support import *
global globvar # had hoped this would do the trick (as it does when
placed inside a function call)
glob = "new"

print "top level(): glob==" +glob

fun() # defined inside support

Calling main.py outputs

top level(): glob==new
fun(): glob==old

How can I get the assignment to globvar performed inside module main.py
to be
felt by the code in support.py?

Thanks in advance!

Best

Juergen Lemke

Oct 29 '06 #1
1 2332
le***********@yahoo.de schrieb:
Hi everyone,

I define some vars and functions in a "support" module which gets
called from my
main app module. Using Python 2.5.

I import all symbols in the support module at the top of the main
module through:

from support import *

Is there a way for me to modify a top-level ("global"?) variable
defined in module
support from the code in the main module and still have those changes
visible to
code in the support module?
No. The from foo import * will create local bindings of the
module-globals in the importing module, and there is no implicit link to
the module's names.

This is the main reason why the "from foo import *"-form is frowned
upon, and you should refrain from using it.

Use e.g.

import support as s

instead, to get a shorter name for referencing the support module.

Diez
Oct 29 '06 #2

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

Similar topics

3
by: gerald.maher | last post by:
Hi, I am trying to excuate the follwong code: I get the following error: Here is my Lib path:
1
by: Darran Edmundson | last post by:
I was playing around with the inspect module the other day trying to write a quick and dirty "smart" logger. By this I mean that writing a message to the global log would also yield some info...
1
by: Xiao Jianfeng | last post by:
Hello, In pymol I can use "from chempy import Atom" but "import chempy.Atom" doesn't work. It says,"ImportError: No module named Atom". What is going wrong ? Thanks
0
by: robert | last post by:
I want to occasionally do a "redirected" import of a python module which is located offside of the legacy sys.path (Windows) - for example a tool is maintained within "cgi-bin" web stuff (while I...
5
by: mark_galeck_spam_magnet | last post by:
Hi, why does complain name 'compileFile' not defined. But works. Why? (I did read the tutorial, it seems to say "import module" should work. Thank you, Mark
2
by: Gabriele *darkbard* Farina | last post by:
Hi, I'm using Python 2.5 to develop a simple MVC framework based on mod_python. To load my controllers, I create new modules using the "new" module like this: # .... my_module =...
0
by: =?ISO-8859-1?Q?Luis_M._Gonz=E1lez?= | last post by:
I apologize for this very basic question, but I can't understand how this works... I want to import a function from module B into my main script A, so this function can see and use the locals from...
1
by: Malcolm Greene | last post by:
Is there any consensus on what "from __future__ import" options developers should be using in their Python 2.5.2 applications? Is there a consolidated list of "from __future__ import" options to...
1
by: Eric Hanchrow | last post by:
(This is with Python 2.5.2, on Ubuntu Hardy, if it matters.) This seems so basic that I'm surprised that I didn't find anything about it in the FAQ. (Yes, I am fairly new to Python.) Here are...
3
by: J. Cliff Dyer | last post by:
On Thu, 2008-05-08 at 12:00 -0700, Eric Hanchrow wrote: It's the same reason as this: 5 5 6 5 Python "variables" are just names that point at objects. When you
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.