1
def func(a, b, c='four'):
    print 'a is %s, b is %s, c is %s' %(a, b ,c)

func('one','two','three')
func('one', 'two')

This code runs with no problem. But what is this called? 'overloading'?

btw, is this style only available in Python? Thank you!

xiaolong
  • 3,396
  • 4
  • 31
  • 46

4 Answers4

5

Python calls those 'default argument values'. It is a very simple form of function overloading (overloading on 'arity' (i.e. how many arguments the function has)). This is as complex as you can get with function overloading in Python though.

Because of a combination of a desire to keep the language fairly simple and the fact that Python variables only have a type at runtime, Python cannot overload by type. You can use isinstance to roll your own overloading if you like, but the resulting code is quite messy, and I wouldn't recommend it.

Other languages also have default arguments. C++, for example, also allows default arguments, and C++ explicitly treats default arguments as a form of function overloading.

One thing about Python default arguments that differs from C++ is that Python has named arguments, which means you can explicitly override a particular default value without overriding the others.

For example:

def foo(bar, baz="A baz", qux=0):
    return "(%r, %r, %r)" % (bar, baz, qux)

foo(5)
(5, 'A baz', 0)
foo(5, qux=2)
(5, 'A baz', 2)

There are other languages that allow this as well. I believe, for example, that this is something Ruby allows, though I'm not sure what the syntax is.

Lastly, Python, weirdly enough, has operator overloading. This is managed through functions with special reserved names that are part of a class. Such functions always have the form __name__. The rules for exactly how operator overloading works in the case of any individual operator can be a little odd, and I would generally recommend against using this feature in most cases, especially for overloading numeric or relational operators.

Omnifarious
  • 54,333
  • 19
  • 131
  • 194
  • Overall this is a great answer; I like your discussion of named arguments. But I don't agree with some of your comments on operator overloading. Operator overloading is great when your class represents some sort of number or group of numbers, and if the math operations are well-defined then operator overloading is the best way to offer those math operations. If complex numbers weren't already built-in, I would overload `+`, `-`, and the other operators so that complex numbers could be used in ordinary expressions. Likewise SciPy has overloaded the operators for matrices and such. – steveha Mar 16 '12 at 01:28
  • @steveha - I largely agree with that. Though most people don't write classes that emulate numbers all that often. Also, it's still tricky because you have to overload the right combination of operators to get the behavior everybody would expect and you can get some surprising results if you don't. But that's always the case with operator overloading. At least Python has the `containers` module which makes it easy to overload a minimal set of operators to get container-like behavior. – Omnifarious Mar 16 '12 at 04:31
  • @Omnifarious, I definitely do agree that operator overloading must be done carefully. – steveha Mar 16 '12 at 19:23
2

No, this is an example of default argument values.

Default argument values are available in many languages.

srgerg
  • 18,719
  • 4
  • 57
  • 39
1

As others have noted, this is default arguments, not operator overloading.

The way Python does actual operator overloading is to have special method functions that you can optionally define in a class. Because Python does it this way, it is not possible to overload operations purely on built-in types; you cannot overload the + operator in the expression 1 + 1. However, you can make a subclass of int and overload that subclass to do whatever you want. MyClass(1) + 1 could possibly have an overloaded + operator.

For more information on operator overloading in Python:

operator overloading in python

Community
  • 1
  • 1
steveha
  • 74,789
  • 21
  • 92
  • 117
  • I think your answer is a bit better than mine, even though I consider default arguments to be a form of overloading. :-) – Omnifarious Mar 15 '12 at 06:03
0

It's called default argument values.

It's not a Python-only feature, one example is C++ and there are more.

Python doesn't support function overloading (unlike C++). Function overloading is when you have more than one function with the same name and compiler figures out which one you're calling based on the number of passed arguments and their types.

yak
  • 8,851
  • 2
  • 29
  • 23