Splitting up classes between files

Skip Montanaro skip at mojam.com
Mon Jan 3 13:15:34 EST 2000


    Roy> I've got a top-level class, a, and a bunch of sub-classes, a1, a2,
    Roy> a3, etc.  To make it easier to manage the code, I'd like to put
    Roy> each in a separate file, but still have the user be able to import
    Roy> the whole bunch with a single import, i.e.  not have to do "import
    Roy> a, a1, a2, a3, etc".

    Roy> Can I just put "import a1, a2, a3, etc" in the bottom of my "a.py"
    Roy> file?  It seems like it should work fine, but are there any hidden
    Roy> gotchas?

Roy,

You might want to look at package support, but without it, I think you can
accomplish what you want.  At the end of a.py, simply add

    from a1 import a1
    from a2 import a2
    from a3 import a3

When your users import module a, they'll get access to

    a.a (class a in python file a.py)
    a.a1 (class a1 from python file a1.py)
    ...

Skip Montanaro | http://www.mojam.com/
skip at mojam.com | http://www.musi-cal.com/
847-971-7098   | Python: Programming the way Guido indented...
 




More information about the Python-list mailing list