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

vicki at thepenguin.org vicki at thepenguin.org
Fri Aug 20 23:03:50 CEST 2004


> vicki at thepenguin.org wrote:
>> Okay, I have written a logging function to allow me to pass a string and
>> a
>> mask to determine which places to log to. The LogMessage function is not
>> contained within a class and looks like this:
>>     [...]
>> If I call this function from anywhere within a class in the same file, I
>> can use it fine. I created a second file with a function defined in it,
>> and I can't call the LogMessage function from there.
>
> You just need to import the file that LogMessage is in.
>
> Say that LogMessage is in a file called MyLog.py.  Now, in another
> file that you want to use it in, you can simply do this:
>
> import MyLog
>
> # ....
>
> Mylog.LogMessage("Foo!")
>
> Hope that helps...
>
> Jeff Shannon

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? I
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? I might decide
to change each function call, but I would really like to know how to do
this anyway.

--Vicki

To simplify, I have this:

file A: function1, Class1 with other functions which call function1
file B: function2 which calls function1 but can't see a variable which was
declared as global inside Class1.




More information about the Tutor mailing list