overloading constructor in python?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • lennart@kommunicera.umea.se

    #1

    overloading constructor in python?


    This is probably a really stupid question, but I cant seem to find a
    satisfying answer by myself so here it goes. In for example java we
    could create a class dummie with several constructors, say one that
    takes an int, and one that takes a String as argument. In python it
    doesnt seem possible to have several __init__ methods ( and I assume if
    we could there would be some problem to determine which __init__ to
    use). So my question is how this is normally solved in python? I dont
    really like the idea of using neither

    def __init__(self, o):
    if type(o) is ...

    nor subclasses for the baseclass, but I cant think of another way. Any
    thoughts anyone?

    Thanx
    /Lennart

  • Larry Bates

    #2
    Re: overloading constructor in python?

    lennart@kommuni cera.umea.se wrote:[color=blue]
    > This is probably a really stupid question, but I cant seem to find a
    > satisfying answer by myself so here it goes. In for example java we
    > could create a class dummie with several constructors, say one that
    > takes an int, and one that takes a String as argument. In python it
    > doesnt seem possible to have several __init__ methods ( and I assume if
    > we could there would be some problem to determine which __init__ to
    > use). So my question is how this is normally solved in python? I dont
    > really like the idea of using neither
    >
    > def __init__(self, o):
    > if type(o) is ...
    >
    > nor subclasses for the baseclass, but I cant think of another way. Any
    > thoughts anyone?
    >
    > Thanx
    > /Lennart
    >[/color]
    In python it is called duck typing but you don't need separate
    constructors:

    def __init__(self, c):
    if isinstance(c, int): ...do stuff...
    elif isinstance(c, list): ...do stuff...
    elif isinstance(c, tuple): ...do stuff...
    else:

    Comment

    • lennart@kommunicera.umea.se

      #3
      Re: overloading constructor in python?


      Larry Bates wrote:
      [...][color=blue]
      > In python it is called duck typing but you don't need separate
      > constructors:
      >
      > def __init__(self, c):
      > if isinstance(c, int): ...do stuff...
      > elif isinstance(c, list): ...do stuff...
      > elif isinstance(c, tuple): ...do stuff...
      > else:
      > .
      > .
      > .
      >
      > Remember Python doesn't care what c is and doesn't do
      > type checking so you can do anything you want.
      >[/color]

      Thanx for the info.

      /Lennart



      [color=blue]
      > -Larry Bates[/color]

      Comment

      • Jarek Zgoda

        #4
        Re: overloading constructor in python?

        Larry Bates napisa³(a):
        [color=blue]
        > In python it is called duck typing but you don't need separate
        > constructors:
        >
        > def __init__(self, c):
        > if isinstance(c, int): ...do stuff...
        > elif isinstance(c, list): ...do stuff...
        > elif isinstance(c, tuple): ...do stuff...
        > else:
        > .
        > .
        > .
        >
        > Remember Python doesn't care what c is and doesn't do
        > type checking so you can do anything you want.[/color]

        This is no better than type checking. And this doesn't remind "duck
        typing" at all.

        --
        Jarek Zgoda

        Comment

        • Paddy

          #5
          Re: overloading constructor in python?

          I guess in python we use two idioms to cover most of the uses of
          mulltiple constructors:
          1) Duck typing were for example if you do:

          class C1(object):
          def __init__(self, n):
          n = float(n)

          n could be an integer, a floating point number,, or a string
          representing a float.

          2) Default values to parameters:

          class C2(object):
          def __init__(self, x=None, y=None, s=""):
          pass

          C2 can be initialised with any one, two, three, or zero arguments;
          those not given would take the default values shown to the right of the
          equals sign above.

          - Cheers, Paddy.
          --
          Signs of spring here in the west-country.

          Comment

          • Alex Martelli

            #6
            Re: overloading constructor in python?

            Paddy <paddy3118@nets cape.net> wrote:
            [color=blue]
            > I guess in python we use two idioms to cover most of the uses of
            > mulltiple constructors:
            > 1) Duck typing were for example if you do:
            >
            > class C1(object):
            > def __init__(self, n):
            > n = float(n)
            >
            > n could be an integer, a floating point number,, or a string
            > representing a float.
            >
            > 2) Default values to parameters:
            >
            > class C2(object):
            > def __init__(self, x=None, y=None, s=""):
            > pass[/color]

            .... and classmethods. OK, _three_ idioms. Oh, and factory functions.
            Among the idioms we use are the following...


            Alex

            Comment

            • Jon Ribbens

              #7
              Re: overloading constructor in python?

              In article <1hd8g42.t78sge 1tuys90N%aleaxi t@yahoo.com>, Alex Martelli wrote:[color=blue][color=green]
              >> I guess in python we use two idioms to cover most of the uses of
              >> mulltiple constructors:[/color]
              >
              > ... and classmethods. OK, _three_ idioms. Oh, and factory functions.
              > Among the idioms we use are the following...[/color]

              Nobody expects the Spanglish inquisition...

              Comment

              Working...