Why does Python not support function overloading?

Source: Internet
Author: User
0 reply content: This question has recently been discussed in cpyug. Let me give a brief summary.

To consider why python does not provide function overloading, first we need to study why function Overloading is required.

Function Overloading is mainly used to solve two problems.
1. Variable Parameter type.
2. Variable Parameter count.

In addition, a basic design principle is that function Overloading is used only when two functions have the same function except for different parameter types and number of parameters, if the functions of the two functions are actually different, you should not use overload, but use a function with different names.

Well, in Case 1, the function functions are the same, but the parameter types are different. How does python handle this? The answer is that it does not need to be processed at all, because python can accept any type of parameters. If the functions are the same, different parameter types may be the same code in python, there is no need to create two different functions.

In Case 2, functions are the same, but the number of parameters is different. How does python handle this? As you know, the answer is the default parameter. You can solve the problem by setting the missing parameters to the default parameters. Because you assume that the functions are the same, the missing parameters are all needed.

Well, given that both scenario 1 and scenario 2 have solutions, python naturally does not need function overloading. I add that Python3 can use metaclass + parameter annotation to make a class Pretend to support function overloading:

class Spam(metaclass=MultipleMeta):    def bar(self, x:int, y:int):        print('Bar 1:', x, y)    def bar(self, s:str, n:int = 0):        print('Bar 2:', s, n)# Example: overloaded __init__import timeclass Date(metaclass=MultipleMeta):    def __init__(self, year:int, month:int, day:int):        self.year = year        self.month = month        self.day = day    def __init__(self):        t = time.localtime()        self.__init__(t.tm_year, t.tm_mon, t.tm_mday)
Who said no? You cannot implement a simple multiple dispatch by yourself. Guido Gave example [1] seven years ago. Students need to read more books and do not bury them in the pitfalls dug by questions ......

Function overloading is required for static languages mainly to solve the call flexibility problem. Using function overloading in a flexible scripting language such as Python is simply an option.

[1]: http://www.artima.com/weblogs/viewpost.jsp? Thread = 1, 101605 Do other languages support such abnormal features? Excuse me:
Cos (float) cos (double) compared with cosf (float) cosd (double), where is the ease of use of the former? Can code be simpler? Python supports optional parameters, such
Def func (a, B = 0, c = 0 ):
Pass
Therefore, calls such as func (1), func (), func (, 3), or func (a = 1, B = 2) are valid, in this case, the function is overloaded.
In my personal opinion, one of the language's choices is heavy load or one of the optional parameters. If C #4 supports both forms at the same time, it is easy to confuse. Phython is a dynamic binding, that is, it can be reloaded as needed. You do not need to define a special overload. overload is not a multi-state overwrite. Overload refers to the use of the same function name, but the parameter list is different (type and number), there is no need for virtual functions or additional overhead, is to rename the function by the compiler during compilation. For example, int f (int), int f (float); Compile the function name to int f_ I _1 (int) and int f_f_1 (float ). the script language does not need to overload the Mechanism. The type of the input parameter itself is not determined. Since the type can be arbitrary, it will not improve the performance, and it will be meaningless. For example, sub f (a) of python: any type can be used to use fdef max (** kwargs) for compaction. Because python is a dynamic language, why not?

Def max (a, B, c = 'nothing '):

If c = 'nothing ':

If a> B:

Print

Else:

Print B

Else:


......
Is this more complicated than heavy load?

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.