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

vicki at thepenguin.org vicki at thepenguin.org
Fri Aug 20 21:42:53 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...

Okay Jeff, I thought of this, but it seemed wrong since I would be
importing the original file that contains the function which is calling
the function which is attempting to log (in the house that Jack built).
Let me be more clear. I have file A which contains most of the code for my
program and file B which only contains the new function. Code in file A
calls the function in file B which then calls the Logging function which
is in file A. I could just move the new function into file A eliminating
the whole problem, but I would rather learn the proper way to do this. I
think part of the problem is that this particular implementation does not
lend itself to OOP principles.


More information about the Tutor mailing list