is it possible to dividing up a class in multiple files?

John McMonagle jmcmonagle at velseis.com.au
Mon Aug 7 23:55:43 EDT 2006


On Mon, 2006-08-07 at 15:41 +0200, Martin Höfling wrote:
> Hi there,
> 
> is it possible to put the methods of a class in different files? I just 
> want to order them and try to keep the files small.
> 

Here's how I do it:

Firstly, I create a file called imports.py which contains all my import
statements.  These are obviously indented at the zero position.

I then create a classname.py file.  This just has the class statement
(eg: class Foo:)
.

I then create a separate file for each function in the class.  Eg:
init.py, function1.py, etc).  These must be indented by one indent
position.

I then create a main.py file.  This contains statements to be executed
after all the statements in the class.

I create a shell script which concatenates the files in the correct
order

eg:

cat imports.py \
classname.py \
init.py \ 
function1.py \
...
...
..
main.py > module.py

I use the concatenated file, module.py, as the program to run or import.

I find it easier to maintain code in this way - if I need to make a
change to a particular function, I just edit the file in which the
function is defined and re-run the shell script to make the program.  If
I need to add a new function to the class I just need to create a new
file and add its entry to the shell script in the correct place and
re-run the shell script.

Of course you are not limited to a single function per file, but that is
what works best for me.

Regards,

John



-- 
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.




More information about the Python-list mailing list