473,788 Members | 2,713 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"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 2356
le***********@y ahoo.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
3912
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
2189
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 about the calling scope. If you look at my function "test" below, I'd ideally like log messages: foo.py:test, "message 1" foo.py:Foo.__init__, "message 2" foo.py:Foo.bar, "message 3"
1
2600
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
1111
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 do not want to add that whole folder's stuff to the sys.path) ..pth adds only directories to the python path. is there a convenient/short method to create a python "module pointer" (file) to such module - maybe just create such pointer file...
5
2188
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
1316
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 = new.module("random_name") my_module.__file__ = module_path
0
942
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 A. For example: def auto(): urls = for k,v in __main__.locals().items(): # these "locals"
1
2159
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 choose from? Thank you, Malcolm
1
1996
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 three tiny files: ==== mut.py ==== import system
3
1694
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
10366
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10175
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9969
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7518
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5399
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3675
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.