[Tutor] executing a function in a different file and class
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
> function after the import, but it doesn't see the logfile name which
> declared as global within the class inside file A
I'm not sure what you mean here but if you just used the global
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
> 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
> truly global in the program space rather than just in the class
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,
or to import specific names
from someModule import someVariable
or to import all names - usually a bad idea:
from someModule import *
> know the caveats of globals and believe that in this case, I am
> 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.
More information about the Tutor