Python does not support function overloading

Source: Internet
Author: User

Tag: Use variable parameter code ref to overload class with the HREF study method

function overloading with Python:

The advantage of function overloading is that you don't write multiple functions for different parameter types or number of arguments. Multiple functions use the same name, but the parameter table, that is, the number of parameters and the data type can be different. Call, although the method name is the same, but according to the parameter table can automatically call the corresponding function. In a static language, method overloading is the possibility that a class can handle different types of data in a uniform manner. In the dynamic language, there is the duck type, that is, if it walks like a duck, it is like a duck, then it is a duck. The characteristics of an object are not determined by its type, but by the methods in the object, so function overloading is meaningless in a dynamic language.

To consider why Python does not provide function overloading, let us first examine why we need to provide function overloading.

function overloading is mainly to solve two problems.
1. The variable parameter type.
2. The number of variable parameters.

In addition, a basic design principle is that only if the function of the two functions is identical except for the parameter type and the number of parameters, then the function overload is used, if the function of two functions is different, then the overload should not be used, but a function with different names should be used.

Okay, so for scenario 1, the functions are the same, but the parameter types are different, how does python handle it? The answer is that there is no need to deal with it, because Python can accept any type of argument, and if the function is the same, then the different parameter types are probably the same code in Python, and there is no need to make two different functions.

So for scenario 2, the function is the same, but the number of parameters is different, how does python handle it? As you know, the answer is the default parameter. Setting the default parameters for those missing parameters solves the problem. Because you assume that functions function the same, then those missing parameters are needed.

Well, given that scenario 1 and Scenario 2 have a solution, Python naturally does not need function overloading.

The above-cited insight: http://www.zhihu.com/question/20053359

Python does not support function overloading

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.