Using “new” style classes (I’m in python 3.2) is there a way to split a class over multiple files? I’ve got a large class (which really should be a single class from an object-oriented design perspective, considering coupling, etc, but it’d be nice to split over a few files just for ease of editing the class.
Share
If your problem really is just working with a large class in an editor, the first solution I’d actually look for is a better way to break down the problem. The second solution would be a better editor, preferably one with code folding.
That said, there are a couple of ways you might break up a class into multiple files. Python lets you use a folder as a module by putting an
__init__.pyin it, which can then import things from other files. We’ll use this capability in each solution. Make a folder called, say,bigclassfirst.In the folder put the various
.pyfiles that will eventually comprise your class. Each should contain functions and variable definitions for the eventual class, not classes. In__init__.pyin the same folder write the following to join them all together.This has the advantage that you end up with a single class derived directly from
object, which will look nice in your inheritance graphs.You could use multiple inheritance to combine the various parts of your class. In your individual modules you would write a class definition for
Bigclasswith parts of the class. Then in your__init__.pywrite:If the multiple inheritance becomes an issue, you can use single inheritance: just have each class inherit from another one in chain fashion. Assuming you don’t define anything in more than one class, the order doesn’t matter. For example,
classdef2.pywould be like:classdef3would importBigclassfromclassdef2and add to it, and so on. Your__init__.pywould just import the last one:I’d generally prefer #1 because it’s more explicit about what members you’re importing from which files but any of these solutions could work for you.
To use the class in any of these scenarios you can just import it, using the folder name as the module name:
from bigclass import Bigclass