Is there function overloading in python

No function overloading in python

In order to consider why python does not provide function overloading, first of all we have to study why it is necessary to provide function overloading.

Function overloading is mainly to solve two problems:

Variable parameter type.

The number of variable parameters.

In addition, a basic design principle is that only when the functions of the two functions are exactly the same except for the different parameter types and number of parameters, then function overloading is used. If the functions of the two functions are actually different, then the functions of the two functions are different. Overloading should be used, but a function with a different name should be used.

So for case 1, the function has the same function, but the parameter type is different, how does python handle it?

The answer is that there is no need to deal with it at all, because python can accept any type of parameter. If the function of the function is the same, then different parameter types are likely to be the same code in python, and there is no need to make two different functions.

So for case 2, the function has the same function, but the number of parameters is different, how does python handle it?

The answer is the default parameters. The problem can be solved by setting the missing parameters as default parameters. Because you assume that the functions are the same, the missing parameters will eventually be needed. Well, given that both cases 1 and 2 have solutions, python naturally does not need function overloading.

Knowledge point supplement:

Suppose you have a function connect, which has a parameter address. This parameter may be a string or a tuple. E.g:

connect('123.45.32.18:8080')connect(('123.45.32.18',8080))

You want to be compatible with these two ways of writing in the code, so you might write the code like this:

def connect(address):ifisinstance(address, str):
ip, port = address.split(':')
elif isinstance(address, tuple):
ip, port = address
else:print('Incorrect address format')

So far, this article on Is there any function overloading in python is introduced here. For more detailed explanations of relevant python function overloading, please search ZaLou.Cn

Recommended Posts

Is there function overloading in python
Is there a helper function in python
Is there an algorithm in python language
What is an anonymous function in Python
Join function in Python
What is the function of adb in python
What is introspection in python
What is object-oriented in python
Why doesn't Python support function overloading?
What is list comprehension in python
There are several keywords in Python
Everything in Python is an object
How to run id function in python
What is the id function of python
What is a sequence table in Python
Is a number in python a variable type
What is the function body of python
Python is mainly used in which directions
Python 3.9 is here!
Functions in python
Python enumerate() function
Python function buffer
How to use the round function in python
How to use the zip function in Python
How to use the format function in python
Why python is popular
Python is short-crawling music
03. Operators in Python entry
Python is slowly fading
Python custom function basics
12. Network Programming in Python3
print statement in python
Python built-in function -compile()
Python function basic learning
Python data analysis-apply function
Python3 built-in function table.md
Concurrent requests in Python
Install python in Ubuntu
Context management in Python
Arithmetic operators in python
Write gui in python
Python Print print timer function
MongoDB usage in Python
Str string in Python
Computational Geometry in Python
Concurrent requests in Python (part 2)
Python high-order function usage summary!
Subscripts of tuples in Python
Is python an interpreted language?
Talking about inheritance in Python
Is python an interpreted language
Noteworthy update points in Python 3.9
Python is short-world epidemic map
Python tornado upload file function
Python magic function eval () learning
How Python implements FTP function
Containerize Python applications in 3 minutes
Python is short _SVM test
Is python code case sensitive
What is Python variable scope
Generators and iterators in Python