Class design issues: multiple constructors

Greg Brunet gregbrunet at NOSPAMsempersoft.com
Thu Aug 7 04:17:40 CEST 2003


I'm trying to write a package to handle working with DBase files.
There's enough different stuff in handling them (unfortunately) that I'm
not going to try to the interface DB-API 2.0 compliant - it'll be a
lower level wrapper around reading & writing the DBF files.  In trying
to design a good framework, however, I'm unsure of how to progress.  I
intend to have a class to represent the actual DBF files, and I would
expect to have an __init__ method to be called when creating a new
instance.  When I first started, I included a filename as a parameter
that would be used to open an existing DBF file.  Next I decided to add
the ability to create a new DBF file.  This method needs additional
parameters (such as the field definitions), and, while in some other
languages, I could provide 2 versions of the constructor (overload it if
I'm using the right terminology), and the compiler would use the
appropriate one, things don't seem to work that way in Python.  I'm also
thinking that I might rather name the methods more specifically (such as
Open & Create) instead of both being __init__.  What would be the
Pythonic way to go about doing this?  Would I make an __init__, Open, &
Create methods, and make 2 calls for each DBF object, like this:

class dbf:
    __init__(self):
        pass
    Create(self, filename, fieldDefs):
        pass
    Open(self, filename):
        pass

# open existing file
f1 = dbf()
f1 = dbf.Open('customer.dbf')
# create a new file
f2 = dbf()
f2 = dbf.Create('states.dbf',  [('StateCode', 'C', 2), \
    ('StateName','C',20)]


Or maybe there's a clean way to be able to 'bundle' the
initialization/construction into the Create & Open method calls.  Thanks
for the help,

-- 
Greg





More information about the Python-list mailing list