[Tutor] executing a function in a different file and class

Jeff Shannon jeff at ccvcorp.com
Fri Aug 20 23:47:21 CEST 2004


vicki at thepenguin.org wrote:

> Okay, now I have a complication from this same question. I can call the
> function after the import, but it doesn't see the logfile name which was
> declared as global within the class inside file A but is not global enough
> to be seen by the function in file B. I guess I could pass the filepointer
> to the function in file B but that will require me to change all the calls
> in both files to this function. Is there a way to make the filepointer
> truly global in the program space rather than just in the class space? 

Well, following my other advice (to put the logging function in an 
entirely separate module), you should move the logfile name into that 
module as well.  Conceptually, your main code shouldn't care *where* 
the log is kept; that's all the responsibility of your logging module.

You might want to have another function in that logging module that'll 
allow you main code to set the filename (and maybe the location). 
This function would have to be called before you started logging 
anything, of course, but it could be as simple as modifying a 
logging-module global variable.

By the way, are you at all familiar with the logging package that's 
included in Python 2.3+ ?  (It's also available for 2.2, but not 
included.)  It's very flexible, and can be used in a fairly simple 
manner.  (Of course, even if you *do* switch over to using the 
standard logging package, it's still a good thing to discuss the 
principles behind separating code into multiple files...)

Jeff Shannon
Technician/Programmer
Credit International




More information about the Tutor mailing list