overloading a function

Andy Gimblett gimbo at ftech.net
Thu Mar 7 08:55:02 EST 2002


On Fri, Mar 08, 2002 at 12:36:46AM +1100, Dave Harrison wrote:

> If I have a function, but I want to be able to call it two different
> ways - the ways being differentiated by the paramaters I pass the
> function) how do I do this ??

The pythonic way is to use default parameter values as follows:

def Foo(num, dflt=None):
    if dflt is None:
        # do whatever is appropriate
    else:
        # do whatever is appropriate

Then you can use either of the following:

Foo(5)
Foo(5, 'hello')

This is actually an FAQ: http://www.python.org/doc/FAQ.html#4.75

HTH,

Andy

-- 
Andy Gimblett - Programmer - Frontier Internet Services Limited
Tel: 029 20 820 044 Fax: 029 20 820 035 http://www.frontier.net.uk/
Statements made are at all times subject to Frontier's Terms and
Conditions of Business, which are available upon request.




More information about the Python-list mailing list