many constructors in a class?

Phil Le Bienheureux phil.le.bienheureux at gmail.com
Wed Aug 14 10:46:33 EDT 2013


2013/8/14 climb65 <climb65 at laposte.net>

> Hello,
>
> here is a small basic question :
>
> Is it possible to have more than one constructor (__init__ function) in a
> class? For instance, to create an object with 2 different ways? If my
> memory is good, I think that with C++ it is possible.
>
> Thanks for your answer.
> --
> http://mail.python.org/mailman/listinfo/python-list
>



Hello,

You have to use default values in __init__ function, like :
def __init__( self, name = None ):
        self.name_ = name

and afterwards in your code, test variable :
if self.name_:
       do something...

Regards,
Phil.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20130814/74c8f152/attachment.html>


More information about the Python-list mailing list