Class design issues: multiple constructors

Miki Tebeka tebeka at cs.bgu.ac.il
Thu Aug 7 07:49:44 EDT 2003


Hello Greg,

> 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:

I see two ways:
1. Module (not class) methods: db = opendb(..), db = createdb(...)
2. Passing keyword arguments. 
    def __init__(selfk, filename, **kw):
        if kw.has_key(create):
            ....

I vote for the former.

HTH.
Miki




More information about the Python-list mailing list