Python overloads and overrides of the "difference"

Source: Internet
Author: User
Overloading and rewriting, which are two new concepts, are two concepts that make it easy for us to confuse each other. 1. overloading (overloading method)
is in a class, the method has the same name, and the parameters are different. What about the return type? can be the same or different. Overloading is a means for a class to handle different types of data in a uniform manner.

function overloading is mainly to solve two problems.
1. 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.
2. Methods override (Overiding method)
Subclasses do not want to inherit the method of the parent class intact, but want to make some changes, this need to adopt the method of rewriting. Method overrides are also called method overrides

Python overloads and overrides of the "difference"

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.