473,394 Members | 1,641 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.

How to deal with globals during refactoring classes into separatefiles.

r0g
Hi There,

I'm refactoring some old code that uses global variables and was
originally written in one big flat file with a view to nicening it up
and then extending it. The problem I have though is when I move the
various classes out to their own separate files and reimport them back
in they can't see the globals in the main program. Most of the globals
were just constants and so I have been able to factor them out but
there's one that's kind of pivotal that I still need, basically a list
of all the threads I have running, I don't see how I could get rid of
that. Example follows...

The original with global variable and classes all in one file...

dirty_global = "whatever"
class example():
def print_message(self):
print "Sure, "+dirty_global
a = example()
a.print_message()
>>>Sure, whatever

But when I put the class in its own file and then import it back in...
from example_class import example
dirty_global = "whatever"
a = example()
a.p()
>>>NameError: global name 'dirty_global' is not defined


I had thought declaring the variable using the global keyword at the top
of the file might make it a global 'proper' (as per the manual) or that
using it at the top of the class file might somehow allow the class to
see the variable in file it was called from but clearly my understanding
of python namespaces and scoping isn't quite there yet as neither of
these seem to make any difference.

How can get my classes to see this 'dirty_global' variable again?

Thanks,

Roger.
Nov 19 '08 #1
3 1973
En Tue, 18 Nov 2008 22:14:39 -0200, r0g <ai******@technicalbloke.com>
escribió:
I'm refactoring some old code that uses global variables and was
originally written in one big flat file with a view to nicening it up
and then extending it. The problem I have though is when I move the
various classes out to their own separate files and reimport them back
in they can't see the globals in the main program. Most of the globals
were just constants and so I have been able to factor them out but
there's one that's kind of pivotal that I still need, basically a list
of all the threads I have running, I don't see how I could get rid of
that. Example follows...

The original with global variable and classes all in one file...

dirty_global = "whatever"
class example():
def print_message(self):
print "Sure, "+dirty_global
a = example()
a.print_message()
>>>Sure, whatever


But when I put the class in its own file and then import it back in...
from example_class import example
dirty_global = "whatever"
a = example()
a.p()
>>>NameError: global name 'dirty_global' is not defined
When example() runs, it sees the globals of the module in which it was
defined. That is, if you print globals() inside the example() function,
you'll see the globals defined in example_class, not the globals in the
main module.
I had thought declaring the variable using the global keyword at the top
of the file might make it a global 'proper' (as per the manual) or that
using it at the top of the class file might somehow allow the class to
see the variable in file it was called from but clearly my understanding
of python namespaces and scoping isn't quite there yet as neither of
these seem to make any difference.

How can get my classes to see this 'dirty_global' variable again?
Put it on another module and import the module wherever you need access to
it.
"global" in Python means "global to the containing module".

--
Gabriel Genellina

Nov 19 '08 #2
On Tue, Nov 18, 2008 at 4:14 PM, r0g <ai******@technicalbloke.comwrote:
Hi There,

I'm refactoring some old code that uses global variables and was
originally written in one big flat file with a view to nicening it up
and then extending it. The problem I have though is when I move the
various classes out to their own separate files and reimport them back
in they can't see the globals in the main program. Most of the globals
were just constants and so I have been able to factor them out but
there's one that's kind of pivotal that I still need, basically a list
of all the threads I have running, I don't see how I could get rid of
that. Example follows...

The original with global variable and classes all in one file...

dirty_global = "whatever"
class example():
def print_message(self):
print "Sure, "+dirty_global
a = example()
a.print_message()
>>>>Sure, whatever


But when I put the class in its own file and then import it back in...
from example_class import example
dirty_global = "whatever"
a = example()
a.p()
>>>>NameError: global name 'dirty_global' is not defined

I had thought declaring the variable using the global keyword at the top
of the file might make it a global 'proper' (as per the manual) or that
using it at the top of the class file might somehow allow the class to
see the variable in file it was called from but clearly my understanding
of python namespaces and scoping isn't quite there yet as neither of
these seem to make any difference.
A `global` declaration at the module-level, while syntactically valid,
doesn't do anything. It only really makes sense to use `global` in
function bodies.
And as Gabriel explained, Python has no program-wide global scope.
Global scope is always module-specific.

Cheers,
Chris
--
Follow the path of the Iguana...
http://rebertia.com
>
How can get my classes to see this 'dirty_global' variable again?

Thanks,

Roger.
--
http://mail.python.org/mailman/listinfo/python-list
Nov 19 '08 #3
r0g
r0g wrote:
Hi There,

I'm refactoring some old code that uses global variables and was
originally written in one big flat file with a view to nicening it up
and then extending it. The problem I have though is when I move the
<snip>

Hi Chris / Gabriel,

Thanks v.much, that's really helped my understanding of this and my
refactoring is now proceeding apace :-)

Cheers,

Roger.
Nov 19 '08 #4

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

Similar topics

10
by: lawrence | last post by:
I get the impression that most people who are using objects in their PHP projects are mixing them with procedural code. The design, I think, is one where the procedural code is the client code, and...
7
by: John | last post by:
Hi, I'm looking for the best way to deal with globals in PHP. As a 'C' software developer, I would normally avoid all globals and not have any at all, but use structs and pass everything in...
2
by: Robin Munn | last post by:
OK, here's another software design question, one that's been bugging me for a while now. What do y'all think is the best way to handle program-level globals, such as configuration option --...
1
by: gianguz | last post by:
In the following example Global is able to create and manage access to objects of any kind (even in a multithreading environment) with and index value attached to. So a Global<0, string> is a...
6
by: Dean Ware | last post by:
Hi, I am part way through developing a C++ application. I am developing it on my own and using VC++ 6.0. I am now at the stage where I wish to start tidying up my code. I have lots of...
8
by: Frank Rizzo | last post by:
I keep hearing this term thrown around. What does it mean in the context of code? Can someone provide a definition and example using concrete code? Thanks.
6
by: marek | last post by:
Hello All, we are doing a quite a big project that contains at the lowest level an unmenaged c++ classes. Above it there are managed wrappers and at the top there are ASP.NET pages. Can anyone...
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 =...
1
by: cokofreedom | last post by:
if __name__ == '__main__': print "Globals (For Loop):" try: for i in globals(): print "\t%s" % i except RuntimeError: print "Only some globals() printed\n" else: print "All globals()...
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: 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...

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.