How to write a constructor accepting initialization and file persistence?

Moshe Zadka m at
Wed Jun 4 19:06:49 CEST 2003

On Wed, 4 Jun 2003, Grzegorz Adam Hankiewicz <gradha at> wrote:

> Since Python doesn't have function overloading, I don't know very
> well what's the best way to write the constructor of an object which
> should accept initialization parameters or an xml.dom.minidom node
> storing it's data.

Use named constructors, of course.
There are several ways to do named constructors in Python, but my
favourite is subclassing:

> class something(parent):
>    def __init__(self, one, two, three, file_data = None)
>       super(something, self).__init__(one, three, file_data)
>       if file_data:
>          init_extra_attributes_from_file_data()
>       else:
>          init_extra_attributes_from_parameters(one, two, thre)

class _something(parent):

class somethingFromParameters(_something):
    def __init__(self, one, two, three):

class somethingFromFileData(_something):
    def __init__(self, file_data):

The user now clearly indicates which constructor he wants to use:

Moshe Zadka --
Buffy: I don't like you hanging out with someone that... short.
Riley: Yeah, a lot of young people nowadays are experimenting with shortness.
Agile Programming Language --

More information about the Python-list mailing list