[Tutor] Classes in separate files
alan.gauld at btinternet.com
Sun Aug 3 01:35:20 CEST 2008
"James" <jtp at nc.rr.com> wrote
> Another question on classes in separate files...
It could just as well be about functions. The issue is about
visibility of names not classes.
> main.py instantiates a class called 'testClass' inside of a file
> In main.py:
> t = temp.testClass()
> So here's a question...how does the object *t* (defined in the
> file) access a global (or even local) variable in main.py? Is it
You would need to import main into temp.py.
But thats OK since your code has to know about main to want
to access one of its variables! But it destroys yuour classes
reuse capability since they are now bound to main...
Otherwise make it a parameter to the method and pass it in from
where it is called in main.
> What if I want the object t to write to a global variable
> inside of main.py...is that possible?
Same as above import the module. Or make the method returm
the value and explicitly set the value.
All of which emphasises why global variables are a bad design
idea. And why class methods should work with their own internal
data as much as possible.
Author of the Learn to Program web site
More information about the Tutor