[Tutor] executing a function in a different file and class
jeff at ccvcorp.com
Fri Aug 20 23:21:25 CEST 2004
vicki at thepenguin.org wrote:
>>You just need to import the file that LogMessage is in.
> 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.
That's a good question, actually. You're getting into design
principles rather than coding principles, here, but it's good to have
an idea about design from the start.
For me, in any case where I have enough code that I want to split it
into more than one file, I try to find all of the conceptual units of
the program, and then put each of those units into a separate file.
Some of those conceptual units may not require much code, so the file
may be pretty short, but that's okay -- better to have a number of
short files, each of which handles a particular topic, than fewer long
files which may combine several concepts. If each file represents a
particular concept, then you can focus on just that when you're
working on that file, and not worry about it when you're working on a
Often, for OO programming, it works well to have each major class have
its own file. Sometimes you'll have several classes that work closely
together, and they can go in the same file -- especially if one or
more of those classes is likely to be only used from another one.
In the case that you mention, I'd pull your LogMessage() function out
into a third file, and have A and B both import that file to use
LogMessage(). It may be that this function is the only thing that's
in your logging module, and that's fine. You might also have some
helper functions for LogMessage(); if so, those would also go in that
More information about the Tutor