Re: Overloading __init__ & Function overloading
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.python archive

Re: Overloading __init__ & Function overloading

From: Paul Rubin <//phr.cx@NOSPAM.invalid>
Date: Fri Sep 30 2005 - 16:29:45 CEST

"Iyer, Prasad C" <prasad.c.iyer@capgemini.com> writes:
> But I want to do something like this
>
> class BaseClass:
> def __init__(self):
> # Some code over here
> def __init__(self, a, b):
> # Some code over here
> def __init__(self, a, b, c):
> # some code here

You can only use one __init__ method. You'd have it count the args:

class BaseClass:
  def __init__(self, *args):
     if len(args) == 2:
        a, b = args
        # some code
     elif len(args) == 3:
        a, b, c = args
        # more code
Received on Sat Oct 15 04:00:18 2005