By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,923 Members | 1,279 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,923 IT Pros & Developers. It's quick & easy.

How to deal with globals during refactoring classes into separatefiles.

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.