constructor overloading like Java ?

Karl Scalet news at yebu.de
Mon May 26 07:57:56 EDT 2003


Hello Markus,

Markus Jais wrote:

> class MyObject
> {
>           public MyObject()
>           {
>                  System.out.println("uno");
>           }
> }
> class MyClass
> {
>           private int a;
>           private MyObject my;
> 
>           public MyClass(int a)
>           {
>                  this.a = a;
>           }
>           public MyClass(MyObject my)
>           {
>                  this.my = my;
>           }
> }

you cannot have more than one contructor, but you could handle
this case inside _the_ constuctor:

class MyClass:
     def __init__(arg):
         if type(arg) == type(0):
             self.a = arg
         elif isinstance(arg, MyObject):
             self.my = arg

in the latter case, arg is a MyObject or a
subclass of it (as with Java)

HTH,
Karl





More information about the Python-list mailing list