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.