Defining a class in several files

Delaney, Timothy tdelaney at avaya.com
Mon May 27 03:28:45 EDT 2002


> From: jb [mailto:jblazi at hotmail.com]
> 
> Is there a way of defining the member functions of a class in 
> different 
> files? If you class is getting large, having to defining all member 
> functions in the same file can be a pain in the neck.

There are a number of ways, including adding methods to existing classes,
etc.

However, probably the best way is to use subclasses.

Split up your methods into (preferably) related groups.

# a.py

class A:

    def method1 (self):
        pass

# b.py

class B:

    def method2 (self):
        pass

# c.py

class C:

    def method3 (self):
        pass

# __init__.py

import a
import b
import c

class D (a.A, b.B, c.C):
    pass

At this point, class D has all the functionality of a.A, b.B and c.C ...
Package it all up (hence the __init__.py) and the outside world may never be
the wiser.

Tim Delaney





More information about the Python-list mailing list