class too big for one file! How can I split it?

Duncan Booth duncan at NOSPAMrcp.co.uk
Thu Apr 10 09:54:20 EDT 2003


meinmailfilter at gmx.de (HHaegele) wrote in 
news:e956b5f1.0304100512.597654d3 at posting.google.com:

> Now, OTLib.py has more then 32000 lines. And that doesn´t work. Python
> 1.5 can´t execute then. So, how can I add the method Btt()? How can I
> make another file, which knows the global XX and who can be handled
> from the ot_starter, like it is in one file? Good would also be, if
> the Btt() can access XX the same way like Att() can. Is that possible?
> Or how must I change the structure?

I strongly recommend you split your class into several smaller classes. 
However, if that really isn't possible, then you could use the execfile 
function. The example below executes each of the included files as though 
it were indented in the class definition even though it is a separate file.

You will get a performance hit on startup though as the included files will 
be compiled every time you run the program instead of being cached in the 
.pyc file.

OTLib.py:
---------------
XX = None

class Ot:
    def __init__(self, a = None, e = None):
        global XX
        XX = e    
    execfile("OTLib_Att.py")
    execfile("OTLib_Btt.py")
---------------

OTLib_Att.py
---------------
def Att(self):
    global XX
    print XX
---------------
OTLib_Btt.py
---------------
def Btt(self):
    global XX
    print XX
---------------

Also though, if I were you I'd drop the global and just make XX a class 
variable you can refer to as Ot.XX, or self.XX if not changing the value. 
If you do that then you can simply define the extra methods in another 
module and assign them into the class. This gets round the compilation hit 
that I described above.

-- 
Duncan Booth                                             duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?




More information about the Python-list mailing list