Why does Python not support function overloading?

Source: Internet
Author: User
Php Chinese network (www.php.cn) provides the most comprehensive basic tutorial on programming technology, introducing HTML, CSS, Javascript, Python, Java, Ruby, C, PHP, basic knowledge of MySQL and other programming languages. At the same time, this site also provides a large number of online instances, through which you can better learn programming... Reply: 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 ):
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 B


Is this more complicated than heavy load?
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.