newbie: constructor question

Sean 'Shaleh' Perry shalehperry at attbi.com
Mon Oct 14 19:22:30 CEST 2002


On Monday 14 October 2002 10:00, Lexy Zhitenev wrote:
> Actually, there is one more way to do it.
>
> class A:
>      def __init__(self, *args):
>          if len(args) == 1: __init__0(self, *args)
>          elif len(args) == 2: __init__1(self, *args)
>          elif len(args) == 3: __init__2(self, *args)
>     def __init__0(self):
>         pass
>     def __init__1(self, a):
>         pass
>     def __init__2(self, a, b):
>         pass
>     def __init__3(self, a, b, c)
>
> Or something like that. Certainly, this code can be a bit optimized, but
> roughly it is so.

another solution is to use the *args definition and then do:

if type(args[0]) == type(0):
  # handle number input
elif type(args[0] == type(""):
  # handle string input

etc.




More information about the Python-list mailing list