How to write a constructor accepting initialization and file persistence?
Moshe Zadka
m at moshez.org
Wed Jun 4 13:06:49 EDT 2003
On Wed, 4 Jun 2003, Grzegorz Adam Hankiewicz <gradha at titanium.sabren.com> 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):
pass
class somethingFromParameters(_something):
def __init__(self, one, two, three):
pass
class somethingFromFileData(_something):
def __init__(self, file_data):
pass
The user now clearly indicates which constructor he wants to use:
somethingFromParameters(1,2,3)
somethingFromFileData("hello.dat")
--
Moshe Zadka -- http://moshez.org/
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 -- http://www.python.org/
More information about the Python-list
mailing list