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

import and shared global variables

Hi,

I'm implementing a plugin-based program, structured like the example
below (where m1 in the main module, loading m2 as a plugin). I wanted
to use a single global variable (m1.glob in the example) to store some
config data that the plugins can access. However, the output shown
belown seems to imply that glob is *copied* or recreated during the
import in m2. Am I missing something? I thought m1 should be in
sys.modules and not be recreated during the import in m2.

After browsing c.l.p, it seems that this is probably somehow due to the
circular import. However, I do not really see why this should be a
problem here. Interestingly, the problem disappears when I put the code
in m1 in a real main() function instead of "if __name__" etc. Though
this seems to solve my problem, I still want to understand what's
happening.

Thanks,

michael
m1.py:
------
glob = [1]
if __name__ == "__main__":
glob.append(2)
print "m1.main().1:", glob
m2 = __import__("m2")
m2.test()
print "m1.main().2:", glob
------

m2.py:
------
def test():
import m1
print "m2.test():", m1.glob
-----

Output:
m1.main().1: [1, 2]
m2.test(): [1]
m1.main().2: [1, 2]

Mar 10 '06 #1
3 2161
> I'm implementing a plugin-based program, structured like the example
below (where m1 in the main module, loading m2 as a plugin). I wanted
to use a single global variable (m1.glob in the example) to store some
config data that the plugins can access. However, the output shown
belown seems to imply that glob is *copied* or recreated during the
import in m2. Am I missing something? I thought m1 should be in
sys.modules and not be recreated during the import in m2.


Yes, you are missing that your first glob is in __main__.glob, _not_ in
m1.glob.

To make that happen, use something like this:

glob = [1]
def main():
pass

if __name__ == "__main__":
import m1
m1.main()
Diez
Mar 10 '06 #2
Ah, thanks everybody! I had thought that, although the name was set to
"__main__", the module that was stored in sys.modules was m1
nevertheless, not a copy.
Well, having to write "import m1" inside m1.py seems a bit peculiar -
it's probably nicer to keep the "__main__" module free from stuff that
has to be imported by others. Would a module global.py (defining glob
and imported by whoever needs it) be more pythonic? (I didn't want to do
that because I really want to resist the temptation of introducing
glob1, glob2, glob3...)

michael
To make that happen, use something like this:

glob = [1]
def main():
pass

if __name__ == "__main__":
import m1
m1.main()
Diez


Mar 10 '06 #3
> has to be imported by others. Would a module global.py (defining glob
and imported by whoever needs it) be more pythonic?
I'd say yes.
(I didn't want to do
that because I really want to resist the temptation of introducing
glob1, glob2, glob3...)


I miss seeing what that has to do with creating a special module.

Diez
Mar 11 '06 #4

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

Similar topics

1
by: Dan Williams | last post by:
Hi people I've just joined the Python list and although I'm sure my question must be asked fairly frequently, I have been unable to find an answer after two days of searching. For the record, my...
4
by: MackS | last post by:
Hi I'm new to Python, I've read the FAQ but still can't get the following simple example working: # file main_mod.py: global_string = 'abc' def main():
1
by: Imran Aziz | last post by:
Hello All, How can I create a Global shared function that automatically gets shared in all ASP.net files. The issue is that when a user comes to the site I am creating, I check if the user is...
15
by: Rob Nicholson | last post by:
A consequence of the ASP.NET architecture on IIS has just hit home with a big thud. It's to do with shared variables. Consider a module like this: Public Module Functions Public GlobalName As...
8
by: Mark Kamoski | last post by:
Hi Everyone-- Please help. What are the implications, (in terms of memory, application footprint, resource use, threading, and so forth), of using Shared methods? These Shared classes raise...
9
by: Bob Day | last post by:
VS 2003, vb.net , sql msde... I have an application with multiple threads running. Its a telephony application where each thread represents a telephone line. For code that would be the same...
1
by: Darin | last post by:
I have an application that is one EXE with mutliple DLL's. In one of the DLL's (libs.dll) that the EXE has refeneced, I have: Public Class Globals Public Shared gLogin as String End Class ...
0
by: Michael Brenner | last post by:
Hi, I'm implementing a plugin-based program, structured like the example below (where m1 in the main module, loading m2 as a plugin). I wanted to use a single global variable (m1.glob in the...
3
by: ankugoe7 | last post by:
Is it possible to share global variables such that all applications which use shared libraries can see the changes made to the global variables by the other applications.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.