FAQ
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

Search Discussions

  • Larry Bates at Apr 3, 2006 at 7:46 pm

    lennart at kommunicera.umea.se wrote:
    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
    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.

    -Larry Bates
  • Lennart at Apr 3, 2006 at 8:02 pm
    Larry Bates wrote:
    [...]
    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.
    Thanx for the info.

    /Lennart



    -Larry Bates
  • Jarek Zgoda at Apr 3, 2006 at 8:08 pm

    Larry Bates napisa?(a):

    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.
    This is no better than type checking. And this doesn't remind "duck
    typing" at all.
  • Paddy at Apr 3, 2006 at 9:09 pm
    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.
  • Alex Martelli at Apr 4, 2006 at 3:15 am

    Paddy wrote:

    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
    ... and classmethods. OK, _three_ idioms. Oh, and factory functions.
    Among the idioms we use are the following...


    Alex
  • Jon Ribbens at Apr 4, 2006 at 2:41 am

    In article <1hd8g42.t78sge1tuys90N%aleaxit at yahoo.com>, Alex Martelli wrote:
    I guess in python we use two idioms to cover most of the uses of
    mulltiple constructors:
    ... and classmethods. OK, _three_ idioms. Oh, and factory functions.
    Among the idioms we use are the following...
    Nobody expects the Spanglish inquisition...

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedApr 3, '06 at 7:28p
activeApr 4, '06 at 3:15a
posts7
users6
websitepython.org

People

Translate

site design / logo © 2021 Grokbase