In python, there is the idea of overloading but no concept of overloading. So some people say that a language like python does not support function overloading, and some people say that python has an overloading function. In fact, the purpose of overloading in python programming lacks the behavior of overloading, or in other words, python does not need overloading!
python is a dynamic language. There is no need to declare variable types. The function can accept any type of parameters, so it cannot support overloading based on the parameter type. Python does not need it. Consider the type of parameters. These can be judged and processed within the function, and there is no need to write a function. Python has multiple ways to pass parameters. Default parameters/variable parameters/variable keyword parameters can handle the problem of variable parameters in function parameters.
The overloading mechanism added in python3.4
In python3.4, a forwarding mechanism is provided to implement overloading
from functools import singledispatch @singledispatch def function(obj): print('%r'%(obj)) @function.register(int) def function_int(obj): print('Integer: %d'%(obj)) @function.register(str) def function_int(obj): print('String: %s'%(obj)) @function.register(list) def function_list(obj): print('List: %r'%(obj)) if __name__ == "__main__": function(1) function('hello') function(range(3)) function(object)
The above is the detailed content of Does python have overloading?. For more information, please follow other related articles on the PHP Chinese website!