473,785 Members | 2,619 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(s elf):
print "Sure, "+dirty_glo bal
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 1993
En Tue, 18 Nov 2008 22:14:39 -0200, r0g <ai******@techn icalbloke.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(s elf):
print "Sure, "+dirty_glo bal
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******@techn icalbloke.comwr ote:
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(s elf):
print "Sure, "+dirty_glo bal
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
3849
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 the classes are a library of utility code. As such, when I've asked about how to get globals into objects, I've been told that I should pass them in as the parameters to a method. Easy enough if you have some procedural code. This answer I've...
7
1761
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 the function parameters... However, being realistic, I can see that globals can (and do ?) have a place in PHP web scripts.
2
1548
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 -- especially in multi-module programs? Here's one approach: --- main.py --- import my_globals as g import somefuncs
1
2118
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 unique string instance object allocated into the system that can be accessed from any point into the program simply 'creating' another instance of Global<int, T> with the same id and type.Like globals, destruction of these objects is delegated until...
6
3357
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 superfluous code that is hard to be sure about removing. For example, I have classes that are ONLY used in methods that are never
8
2016
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
1315
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 tell me how to force ASP.NET not to "clear" or reinitialize unmenaged global or static variables that are set on unmanaged level between successive requests?
5
1294
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 = msg_class(data) return msg
1
1857
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() printed\n"
0
9481
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10155
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
9953
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...
0
8978
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6741
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
5383
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
5513
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4054
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3655
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.