Multiple constructors (part 2)

Daniel Klein DanielK at jBASE.com
Wed Jan 17 17:21:54 EST 2001


OK, I've researched and read the posts (and the FAQ) on how to simulate
multiple constructors. Alex Martelli provided the most robust answer by
testing the number of *args and executing the appropriate section of code.
However in another post he says

"""
This 'overloads' the constructors based on how many arguments
are given -- how elegant (and how Pythonic...!) this is, being
of course *debatable*.  Overloading on *types* would be less
elegant *and* less Pythonic, though you could easily extend
this idea to do it -- I would discourage it even more strongly.
"""

However, what I need to do is _exactly_ what is being discouraged, that is
creating 3 constructors both with 2 arguments where the second argument of
each is a different type. The real kicker is that in one of the
constructors, I need to check the __class__ of the object to make sure the
method is receiving the proper object. I have no problem coding this if this
is the way it has to be but if there are more acceptable (and Pythonic) ways
to do this, I would appreciate some pointers.

experiencing-python-growing-pains-but-loving-it-ly yr's,
Daniel Klein





More information about the Python-list mailing list