I know python does not allow us to overload functions. However, does it have inbuilt overloaded methods?
Consider this:
setattr(object_name,\'variable\',
The function setattr(foo, 'bar', baz)
is always the same as foo.bar = baz
, regardless of the type of foo. There is no overloading here.
In Python 3, limited overloading is possible with functools.singledispatch, but setattr
is not implemented with that.
A far more interesting example, in my opinion, is type()
. type()
does two entirely different things depending on how you call it:
Nevertheless, type()
is not overloaded. Why not? Because it is implemented as one function that counts how many arguments it got and then decides what to do. In pure Python, this is done with the variadic *args
syntax, but type()
is implemented in C, so it looks rather different. It's doing the same thing, though.