newbie: constructor question

Alan Kennedy alanmk at hotmail.com
Mon Oct 14 12:49:49 EDT 2002


Alexander Eisenhuth wrote:
> 
> is it possible to implement multiple constructors with different no. 
> of arguments for one class ?

In a word, no.

Instead, you have to use  python's default argument capabilities, and a
single constructor, like so

class A:

    def __init__(self, a, b="default", c="default")
        self.a = a
        self.b = b
        self.c = c

>>> o1 = A("str")
>>> o1.a, o1.b, o1.c
("str", "default", "default")
>>> o2 = A("str", "notdefault")
>>> o2.a, o2.b, o2.c
("str", "notdefault", "default")
>>> o3 = A("str", "notdefault", "notdef")
>>> o3.a, o3.b, o3.c
("str", "notdefault", "notdef")

HTH,

alan kennedy
-----------------------------------------------------
check http headers here: http://xhaus.com/headers
email alan:              http://xhaus.com/mailto/alan



More information about the Python-list mailing list