Using Multiple Constructors

jjinkins jjinkiins at midsouth.rr.com
Sat Aug 4 18:11:55 EDT 2001


Assuming the class is in a module, why not write functions to act as alternate
constructors?

def MyObjectNbr(number):
    string = "%s" % number
    return MyObject(string)

Class MyObject:
    def __init__(self, string):
        self.string = string

    Jim Jinkins


Mikhael Loo wrote:

> How do you use multiple constructors in a python class.  I'm guesing that it
> doesn't involve multiple __init__ functions.
>
> Below is my idea but I'm thinking there must be another method that  is
> evading me.
>
> Class MyObject:
>     Constructor1(self, number):
>         pass
>     Constructor2(self, string):
>         pass
>
> Help anyone?




More information about the Python-list mailing list