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

Alan Gauld alan.gauld at blueyonder.co.uk
Sat Aug 21 00:06:26 CEST 2004


> 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

I'm not sure what you mean here but if you just used the global
keyword
that simply means that assignments to that name will affect the module
level variable of the same name. It does nothing to the class per se.

> but is not global enough to be seen by the function in file B.

If File B can see File A(coz it imported it) then it can see the
global variable by preceding it with A

import A

A.globalVar

> I guess I could pass the filepointer to the function in file B

Which is usually a good idea - its why you should try to avoid using
global variables. Passing parameters iis nearly always better.
But it may not be needed here.

> 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?

Python doesn't support true golobal variables but the namespace
control allows you to import the module with makes all names in
it indirectly visible,

import someModule
print someModule.someVariable

or to import specific names

from someModule import someVariable
print someVariable

or to import all names - usually a bad idea:

from someModule import *
print someVariable

> know the caveats of globals and believe that in this case, I am
willing to
> do it anyway. So how does one make a global truly global?

Thats such a yucky idea that Python won't let you, but it does
provide the import controls described above which allow you to
fake it on a file by file basis.

HTH,

Alan G.



More information about the Tutor mailing list