Does python have overloading?

anonymity
Release: 2019-06-15 14:25:29
Original
4010 people have browsed it

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.

Does python have overloading?

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)
Copy after login

The above is the detailed content of Does python have overloading?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template