[Tutor] Classes in separate files

James jtp at nc.rr.com
Sat Aug 2 16:51:03 CEST 2008

Another question on classes in separate files...

main.py instantiates a class called 'testClass' inside of a file temp.py.

In main.py:
   t = temp.testClass()

So now I can access some of the variables inside of 't'. For example,
let's say that in main.py, I do the following:

# get a variable from the t class (I know, this is not the cleanest
way to do this ;))
tempVariable = t.tempVar

So here's a question...how does the object *t* (defined in the temp.py
file) access a global (or even local) variable in main.py? Is it
possible? What if I want the object t to write to a global variable
inside of main.py...is that possible?


On Sat, Aug 2, 2008 at 10:34 AM, James <jtp at nc.rr.com> wrote:
> 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