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

globals accros modules

alf
Hi,
executing main.py reulsts in following:
[andy@localhost andy]$ python main.py
Traceback (most recent call last):
File "main.py", line 4, in ?
amodule.f()
File "/raid/home/andy/amodule.py", line 3, in f
print a
NameError: global name 'a' is not defined

How can I have global globals without cyclical import?

A.
--------------------
amodule.py:
def f():
global a
print a
--------------------
main.py:
import amodule
a=1
amodule.f()
Jan 11 '07 #1
12 1189
At Thursday 11/1/2007 01:11, alf wrote:
>How can I have global globals without cyclical import?
You can't. Globals are module globals. You need to qualify *which*
module `a` belongs to.
>--------------------
amodule.py:
def f():
global a
print a
--------------------
main.py:
import amodule
a=1
amodule.f()
Change a=1 to amodule.a=1
If you find yourself doing tricks with the module globals, think
about redesigning your application.
--
Gabriel Genellina
Softlab SRL


__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas

Jan 11 '07 #2
alf
Gabriel Genellina wrote:
Change a=1 to amodule.a=1
I have multiple command line programs creating 'a' and amodule using it.
Plus some import sequence dependency. So it would not work. Currently
the solution in amodule is:
import __main__
print __main__.a
If you find yourself doing tricks with the module globals, think about
redesigning your application.
For what I do it is good enough. If I hit the same problem, will
refactor it.

A.
Jan 11 '07 #3
>
Change a=1 to amodule.a=1
If you find yourself doing tricks with the module globals, think about
redesigning your application.
Of course I completely agree with you.

But ...
if you're moving from MatLab to Python,
and want to show your collegaes,
with how little effort they can reuse all their existing MatLab routines
in Python,
then the global issue is a real pain !!

You can explain your collegaes, that
- the startindex of arrays changes from 1 to 0
- slices are upto, instead of including the final border
- indention is thé key
And tell them about all beautiful things in Python,
but tell them that they are going to loose all their globals ???

cheers,
Stef Mientki

Jan 11 '07 #4
stef a écrit :
>
>>
Change a=1 to amodule.a=1
If you find yourself doing tricks with the module globals, think about
redesigning your application.
Of course I completely agree with you.

But ...
if you're moving from MatLab to Python,
and want to show your collegaes,
with how little effort they can reuse all their existing MatLab routines
in Python,
then the global issue is a real pain !!

You can explain your collegaes, that
- the startindex of arrays changes from 1 to 0
- slices are upto, instead of including the final border
- indention is thé key
And tell them about all beautiful things in Python,
but tell them that they are going to loose all their globals ???
It's a feature. Globals are definitively a BadThing(tm).
cheers,
Stef Mientki
Jan 11 '07 #5
Bruno Desthuilliers wrote:
stef a écrit :
>>
>>>
Change a=1 to amodule.a=1
If you find yourself doing tricks with the module globals, think
about redesigning your application.
Of course I completely agree with you.

But ...
if you're moving from MatLab to Python,
and want to show your collegaes,
with how little effort they can reuse all their existing MatLab
routines in Python,
then the global issue is a real pain !!

You can explain your collegaes, that
- the startindex of arrays changes from 1 to 0
- slices are upto, instead of including the final border
- indention is thé key
And tell them about all beautiful things in Python,
but tell them that they are going to loose all their globals ???

It's a feature. Globals are definitively a BadThing(tm).
>cheers,
Stef Mientki
Jan 11 '07 #6
Bruno Desthuilliers wrote:
stef a écrit :
>>
>>>
Change a=1 to amodule.a=1
If you find yourself doing tricks with the module globals, think
about redesigning your application.
Of course I completely agree with you.

But ...
if you're moving from MatLab to Python,
and want to show your collegaes,
with how little effort they can reuse all their existing MatLab
routines in Python,
then the global issue is a real pain !!

You can explain your collegaes, that
- the startindex of arrays changes from 1 to 0
- slices are upto, instead of including the final border
- indention is thé key
And tell them about all beautiful things in Python,
but tell them that they are going to loose all their globals ???

It's a feature. Globals are definitively a BadThing(tm).
yes, I know, but my audience will accept that only in the long term.

But maybe this idea works:

<file Ugly_MatLab_Globals.py>
global var1
global var2
</file Ugly_MatLab_Globals.py>
<all other py-files in the project>
import Ugly_MatLab_Globals

def some_function():
import Ugly_MatLab_Globals
</all other files in the project>
(btw who owns the BadThing tm ;-)

cheers,
Stef Mientki
Jan 11 '07 #7
Stef Mientki a écrit :
Bruno Desthuilliers wrote:
>stef a écrit :
(snip)
>>You can explain your collegaes, that
- the startindex of arrays changes from 1 to 0
- slices are upto, instead of including the final border
- indention is thé key
And tell them about all beautiful things in Python,
but tell them that they are going to loose all their globals ???


It's a feature. Globals are definitively a BadThing(tm).


yes, I know, but my audience will accept that only in the long term.
Unless you clearly explain the benefits... Any code relying on the
existence of a global is:
1/ dependent on the existence of this global
2/ harder to understand

FWIW, I'm currently fixing a simple Delphi program that's using quite a
few globals, and since I'm not familiar with ObjectPascal (my experience
with Pascal boils down to a few cs101 stuff like implementing a linked
list, some 6 or 7 years ago), I'm losing a lost of time with these [bip]
globals...
But maybe this idea works:

<file Ugly_MatLab_Globals.py>
global var1
global var2
</file Ugly_MatLab_Globals.py>
The 'global' statement only makes sens within a function, and it's only
a declaration, not a definition (-it won't bind the following name by
itself - only tell the interpreter that this name is to be considered as
belonging to the module's namesepace ).

The minimal working example requires that you assign a default value:

# myglobs.py
meaning_of_life = 42

# another.py
import myglobs
print myglobs.meaning_of_life
>
<all other py-files in the project>
import Ugly_MatLab_Globals

def some_function():
import Ugly_MatLab_Globals
You don't have to reimport it here...
>
(btw who owns the BadThing tm ;-)
I Do(tm) !-)
Jan 11 '07 #8
Bruno Desthuilliers wrote:
Stef Mientki a écrit :
>Bruno Desthuilliers wrote:
>>stef a écrit :
(snip)
>>>You can explain your collegaes, that
- the startindex of arrays changes from 1 to 0
- slices are upto, instead of including the final border
- indention is thé key
And tell them about all beautiful things in Python,
but tell them that they are going to loose all their globals ???
It's a feature. Globals are definitively a BadThing(tm).


yes, I know, but my audience will accept that only in the long term.

Unless you clearly explain the benefits... Any code relying on the
existence of a global is:
1/ dependent on the existence of this global
2/ harder to understand
And you think physicians will believe that ?
And suppose they believe it, are the willing to stop their research to
rethink and rewrite their code ;-)
>
FWIW, I'm currently fixing a simple Delphi program that's using quite a
few globals,
Then it was certainly not written by a Delphi-guy ;-)

and since I'm not familiar with ObjectPascal (my experience
with Pascal boils down to a few cs101 stuff like implementing a linked
list, some 6 or 7 years ago), I'm losing a lost of time with these [bip]
globals...
>But maybe this idea works:

<file Ugly_MatLab_Globals.py>
global var1
global var2
</file Ugly_MatLab_Globals.py>

The 'global' statement only makes sens within a function, and it's only
a declaration, not a definition (-it won't bind the following name by
itself - only tell the interpreter that this name is to be considered as
belonging to the module's namesepace ).

The minimal working example requires that you assign a default value:

# myglobs.py
meaning_of_life = 42
thanks for the tip.
>
# another.py
import myglobs
print myglobs.meaning_of_life
>>
<all other py-files in the project>
import Ugly_MatLab_Globals

def some_function():
import Ugly_MatLab_Globals

You don't have to reimport it here...
Then I miss something:

TEN = 10
TWELVE = 12
def some_function():
global TEN
TEN = 9
TWELVE = 11
print TEN, TWELVE

some_function() #will print 9,11
print TEN, TWELVE #will print 9,12

Or am I mistaken ?

cheers,
Stef Mientki
Jan 11 '07 #9
At Thursday 11/1/2007 11:55, stef wrote:
>Of course I completely agree with you.

But ...
if you're moving from MatLab to Python,
and want to show your collegaes,
with how little effort they can reuse all their existing MatLab routines
in Python,
then the global issue is a real pain !!

You can explain your collegaes, that
- the startindex of arrays changes from 1 to 0
- slices are upto, instead of including the final border
- indention is thé key
And tell them about all beautiful things in Python,
but tell them that they are going to loose all their globals ???
Yes
--
Gabriel Genellina
Softlab SRL


__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas

Jan 11 '07 #10
stef wrote:
>Change a=1 to amodule.a=1
If you find yourself doing tricks with the module globals, think about
redesigning your application.
Of course I completely agree with you.

But ...
if you're moving from MatLab to Python,
and want to show your collegaes,
with how little effort they can reuse all their existing MatLab routines
in Python,
then the global issue is a real pain !!

You can explain your collegaes, that
- the startindex of arrays changes from 1 to 0
- slices are upto, instead of including the final border
- indention is thé key
And tell them about all beautiful things in Python,
but tell them that they are going to loose all their globals ???
Yup. If they don't tell us how to write programs we won't tell them how
to cure illness. Sounds like a deal to me ...

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Blog of Note: http://holdenweb.blogspot.com

Jan 11 '07 #11
Stef Mientki wrote:
Bruno Desthuilliers wrote:
>Unless you clearly explain the benefits... Any code relying on the
existence of a global is:
1/ dependent on the existence of this global
2/ harder to understand
And you think physicians will believe that ?
And suppose they believe it, are the willing to stop their research to
rethink and rewrite their code ;-)
If they give a damn about their research, they will. Sloppy, irreproducible lab
technique is not acceptable. Sloppy, impossible-to-verify code shouldn't be, either.

I'd say more, but Greg Wilson has said it better, and has provided course
materials to teach good programming practices to scientists.

http://swc.scipy.org/

Of particular note are his article in the _American Scientist_:

http://www.americanscientist.org/tem.../assetid/48548

and his slides from his talk at SciPy '06:

http://www.third-bit.com/lectures/scipy06.pdf

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Jan 11 '07 #12
Stef Mientki a écrit :
Bruno Desthuilliers wrote:
>Stef Mientki a écrit :
>>Bruno Desthuilliers wrote:

stef a écrit :

(snip)
>>>>but tell them that they are going to loose all their globals ???

It's a feature. Globals are definitively a BadThing(tm).

yes, I know, but my audience will accept that only in the long term.

Unless you clearly explain the benefits... Any code relying on the
existence of a global is:
1/ dependent on the existence of this global
2/ harder to understand

And you think physicians will believe that ?
Aren't physicians supposed to be somewhat cartesians guys ? FWIW,
functional languages are mostly used by peoples with a strong
mathematical background...
And suppose they believe it, are the willing to stop their research to
rethink and rewrite their code ;-)
Not my problem. But anyway, if they really hope to get reliable results,
they'd better have bug-free programs, and using globals not only
potentially introduces subtle bugs, but also makes code hardly
unit-testable...
>>
FWIW, I'm currently fixing a simple Delphi program that's using quite
a few globals,

Then it was certainly not written by a Delphi-guy ;-)
Yes it was. But that does not imply it was written by a competent
Delphi-guy !-)

(snip)
>>
# another.py
import myglobs
print myglobs.meaning_of_life
>>>
<all other py-files in the project>
import Ugly_MatLab_Globals

def some_function():
import Ugly_MatLab_Globals


You don't have to reimport it here...

Then I miss something:
Probably
TEN = 10
TWELVE = 12
def some_function():
global TEN
TEN = 9
rebinds the 'global' (well... module) name 'TEN'.
TWELVE = 11
creates a *local* name 'TWELVE' and bind it to integer 11.
print TEN, TWELVE

some_function() #will print 9,11
print TEN, TWELVE #will print 9,12

Or am I mistaken ?
You are.

bruno@bibi playground $ cat myglobals.py
a = None
b = None
bruno@bibi playground $ cat useglobals.py
import myglobals

def test():
myglobals.a = "meaning_of_life"
myglobals.b = 42

print "myglobals.a : ", myglobals.a
print "myglobals.b : ", myglobals.b
test()
print "myglobals.a : ", myglobals.a
print "myglobals.b : ", myglobals.b

bruno@bibi playground $ python useglobals.py
myglobals.a : None
myglobals.b : None
myglobals.a : meaning_of_life
myglobals.b : 42
Jan 12 '07 #13

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

Similar topics

9
by: Martin Drautzburg | last post by:
My wxPython program starts execution in mainFrame.py like this class MainApp(wxApp): def OnInit(self): self.mainFrame = MainFrame(None) self.mainFrame.Show() self.SetTopWindow(self.mainFrame)...
6
by: Paddy | last post by:
Hi, I got tripped up on the way eval works with respect to modules and so wrote a test. It seems that a function carries around knowledge of the globals() present when it was defined. (The...
18
by: fred.dixon | last post by:
i have read the book and searched the group too ---------------------------------------------- im not gettin it. i want to read a global (OPTIONS) from file1 from a class method (func1) in file2...
11
by: Matt | last post by:
Hello, I'm rather new to C++ and have a question about globals. What is the big deal about them? They make things much easier to program because you don't have to worry about all the passing...
2
by: DFS | last post by:
Not sure whether it's bad programming practice or not, but I have a module of globals I declare in each system: Global ws As Workspace Global db As Database Global td As TableDef Global rs As...
18
by: robert | last post by:
Using global variables in Python often raises chaos. Other languages use a clear prefix for globals. * you forget to declare a global * or you declare a global too much or in conflict * you...
56
by: Smokey Grindle | last post by:
Whats the easiest way to make a global function in VB 2005? The old way was make a module, is that still the standard? thanks!
5
by: Steven W. Orr | last post by:
I have two seperate modules doing factory stuff which each have the similar function2: In the ds101 module, def DS101CLASS(mname,data): cname = mname+'DS101' msg_class = globals() msg =...
13
by: Bartc | last post by:
I noticed that in C, functions in any module are automatically exported. So that it's not possible to use the same function name in two modules (ie. source files). Now that I know that, I get...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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...
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
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.