RE: Overloading __init__ & Function overloading

Discussion in 'Python' started by Iyer, Prasad C, Sep 30, 2005.

  1. Thanks a lot for the reply.
    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

    baseclass1=BaseClass()
    baseclass2=BaseClass(2,3)
    baseclass3=BaseClass(4,5,3)




    regards
    prasad chandrasekaran










    --- Cancer cures smoking

    -----Original Message-----
    From: Larry Bates [mailto:]
    Sent: Friday, September 30, 2005 7:39 PM
    To: Iyer, Prasad C
    Subject: Re: Overloading __init__ & Function overloading

    I may be reading this question different than Fredrik.

    This example is with old-style classes.

    class baseclass:
    def __init__(self, arg):
    #
    # Do some initialization
    #

    def method1(self, arg):
    #
    # baseclass method goes here
    #

    class myclass(baseclass):
    def __init__(self, arg):
    #
    # This method gets called when I instantiate this class.
    # If I want to call the baseclass.__init__ method I must
    # do it myself.
    #
    baseclass.__init__(arg)

    def method1(self, arg):
    #
    # This method would replace method1 in the baseclass
    # in this instance of the class.
    #

    myObj=myclass(arg)

    I could be way off base, but maybe it will help.

    -Larry Bates



    Iyer, Prasad C wrote:
    > I am new to python.
    >
    > I have few questions
    > a. Is there something like function overloading in python?
    > b. Can I overload __init__ method
    >
    > Thanks in advance
    >
    >
    >
    > regards
    > prasad chandrasekaran
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    > --- Cancer cures smoking
    > -----Original Message-----
    > From: python-list-bounces+prasad.c.iyer=
    > [mailto:python-list-bounces+prasad.c.iyer=] On
    > Behalf Of
    > Sent: Friday, September 30, 2005 6:36 PM
    > To:
    > Subject: Python-list Digest, Vol 24, Issue 455
    >
    > Send Python-list mailing list submissions to
    >
    >
    > To subscribe or unsubscribe via the World Wide Web, visit
    > http://mail.python.org/mailman/listinfo/python-list
    > or, via email, send a message with subject or body 'help' to
    >
    >
    > You can reach the person managing the list at
    >
    >
    > When replying, please edit your Subject line so it is more specific
    > than "Re: Contents of Python-list digest..."
    >
    > This message contains information that may be privileged or

    confidential and is the property of the Capgemini Group. It is intended
    only for the person to whom it is addressed. If you are not the intended
    recipient, you are not authorized to read, print, retain, copy,
    disseminate, distribute, or use this message or any part thereof. If
    you receive this message in error, please notify the sender immediately
    and delete all copies of this message.
    >



    This message contains information that may be privileged or confidential and is the property of the Capgemini Group. It is intended only for the person to whom it is addressed. If you are not the intended recipient, you are not authorized to read, print, retain, copy, disseminate, distribute, or use this message or any part thereof. If you receive this message in error, please notify the sender immediately and delete all copies of this message.
     
    Iyer, Prasad C, Sep 30, 2005
    #1
    1. Advertising

  2. Iyer, Prasad C

    Paul Rubin Guest

    "Iyer, Prasad C" <> 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
     
    Paul Rubin, Sep 30, 2005
    #2
    1. Advertising

  3. Iyer, Prasad C wrote:
    > Thanks a lot for the reply.
    > 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
    >
    > baseclass1=BaseClass()
    > baseclass2=BaseClass(2,3)
    > baseclass3=BaseClass(4,5,3)


    In my experience, the vast majority of cases where you "want" function
    overloading, you really just want sensible default parameters. Since
    Python is dynamically typed, the other common use case in static-typed
    language (to provide f(int,int), f(float,float), f(double,complex),
    f(Momma,Poppa) equivalents) is entirely unnecessary.

    Try:

    class BaseClass:
    def __init__(self, a = None, b = None, c = None):
    if a == None:
    <etc>

    or (if you want to take any number of parameters)

    class BaseClass:
    def __init__(self, *args):
    if len(args) == 0:
    <etc>

    Of course, this is assuming that the behaviour is radically different
    based on the number of arguments, which is generally Poor Design. You
    probably _REALLY_ want:

    class BaseClass:
    def __init__(self, a=SensibleDefault1, b=SensibleDefault2,
    c=SensibleDefault3):
    <etc>

    As a concrete example of this, consider:

    class Point:
    def __init__(self, x=0, y=0, z=0):
    <etc>

    Then you can call it with:
    originPoint = Point()
    pointInX = Point(xloc)
    pointInXYPlane = Point(xloc,yloc)
    pointIn3DSpace = Point(xloc,yloc,zloc)
    <note, normally I loathe CaMeLcAsE, but it's readable in this context>

    Or if the Defaults aren't quite so simple, and sensible defaults depend
    on previous values, use:

    class BaseClass:
    def __init__(self, a=SensibleDefault1, b=None, c=None):
    if b==None:
    b = stuff_involving(a)
    if c==None:
    c = stuff_involving(a,b)
    <etc>
     
    Christopher Subich, Sep 30, 2005
    #3
  4. in python you can provide default values for your parameters:

    class BaseClass:
    def __init__(self, a=None):
    if a is None:
    #no parameter
    pass
    else:
    #one parameter
    pass

    baseclass1=BaseClass()
    baseclass2=BaseClass(1)
     
    Istvan Albert, Sep 30, 2005
    #4
  5. Iyer, Prasad C

    John J. Lee Guest

    Paul Rubin <"http://phr.cx"@NOSPAM.invalid> writes:

    > "Iyer, Prasad C" <> 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


    Weeellll... more readably, you can use:

    1. Named arguments (aka "keywords arguments" -- though a keyword arg
    isn't a keyword, of course...)

    2. Factory (class methods) (I'm using those parentheses around "class
    methods" for precedence, not annotation -- unlike here ;-)

    3. Plain old factory methods

    4. Factory "functions" implemented as classes with a __call__ method

    5. Factory classes with named factory methods (perhaps even class
    methods)

    6. Plain old factory functions


    It's all terribly restrictive, as you can see <wink>

    (Yes, I know the OP used the same name to call all the constructors in
    his examples -- but that's just an expectation carried over from other
    languages)


    John
     
    John J. Lee, Sep 30, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steven Bethard
    Replies:
    2
    Views:
    458
    Steven Bethard
    Feb 16, 2005
  2. Iyer, Prasad C

    Overloading __init__ & Function overloading

    Iyer, Prasad C, Sep 30, 2005, in forum: Python
    Replies:
    3
    Views:
    6,413
    Fredrik Lundh
    Sep 30, 2005
  3. Fredrik Lundh
    Replies:
    0
    Views:
    451
    Fredrik Lundh
    Sep 30, 2005
  4. Steve Holden
    Replies:
    0
    Views:
    431
    Steve Holden
    Sep 30, 2005
  5. Fredrik Lundh
    Replies:
    0
    Views:
    402
    Fredrik Lundh
    Sep 30, 2005
Loading...

Share This Page