[Tutor] Classes in separate files

James jtp at nc.rr.com
Sat Aug 2 16:34:48 CEST 2008


All,

I've started tinkering (just a little) with classes, primarily because
I have to. (I've never been a huge fan of OOP, but can tolerate it
when used properly).

I'll be the first to admit that I don't know much about how to program
"correctly" when dealing with objects, however, so any thoughts in the
matter would be greatly appreciated.

I have a few files that I use as "libraries" which contain dozens of
functions that I use across a wide array of programs. This works fine
when I want to invoke a function, pass in a few parameters, and then
get something back. I'm now trying to put a few classes which I know I
will be using repeatedly in a separate file, as well.

The issue is, however, that I'm not sure the "best" way to pass things
into classes, and the "best" way to get something back.

I have a main file, main.py. It's going to create several instances
that are defined by a class in class1.py. *However*, I also need to
instantiate numerous other classes defined in class2.py.

class1.py has a few classes, such as
 - ticket (represents a ticket opened to fix bugs in a program)
 - ticketAnalyzer (an object who looks at all the tickets opened/available)

class2.py has a few classes, as well, such as:
 - codemonkey (defines a person that is going to take tickets and fix them)
 - codereviewer (defines a person who will double check a codemonkey's work)

main.py has the "main" function, and will what is actually invoked at
the command line. In main.py I can instantiate an object of type
ticket, an object of type ticketAnalyzer, and then instantiate
code{monkey,reviewer} classes. However, how do I get codemonkey and
codereviewer to call methods on the ticket and ticketAnalyzer classes?

The only solution I can think of here is having the main function (in
main.py) which instantiates all these objects actually *pass in* to
the codemonkey and reviewer the reference to the specific objects
ticketAnalyzer and ticket. Is this the best way to do it? Should I
handle that behavior in the __init__ of code{monkey,reviewer}? Should
I instead create a method inside of the codemonkey and reviewer
classes that accepts the object pointer to the ticket objects and then
interact between ticket/code* objects as such?

I image it would be much easier to have everything in one file, but
that goes against my grain. ;)

Thoughts appreciated!
-j


More information about the Tutor mailing list