Why does Python not support function overloading?

Source: Internet
Author: User

Reply content:

This question has been hotly discussed recently in Cpyug. Let me summarize briefly.

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. I add that Python3 is able to make a class by metaclass + parameter annotation pretending 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 says it's not supported? Oneself realizes a simple multiple dispatch not to be finished. [1] Guido was given an example seven years ago. Students to read more, not casually by the problem dug the hole buried ...

Static language needs function overloading is mainly to solve the problem of call flexibility, Python such a flexible scripting language with function overloading is purely superfluous.

[1]: http://www. Artima.com/weblogs/view post.jsp?thread=101605 Are there other languages that support such kinky traits? Excuse me:
What is the ease of use of cos (float) cos (double) and COSF (float) cosd (double)? Can you make your code more concise? Python supports optional parameters, such as
def func (A, b = 0, c = 0):
Pass
So, a call like Func (1), Func (a=1), or func (b=2) is legal, in which case the overload of the function looks like a chicken.
Personally, a language chooses one of the overloaded or optional parameters. Like C # 4, which supports both forms, is more likely to be confusing. Phython is a dynamic binding, which means that it can be arbitrarily overloaded, you do not need to specifically define overloading you are wrong, overloaded overload is not polymorphic overwrite. Overloading refers to the use of the same function name, but the parameter list is different (type and number), it is not required virtual function and does not require additional overhead, is at compile time by the compiler to rename the function. such as int f (int), int f (float); Compiles its possible function names into int f_i_1 (int) and int f_f_1 (float). The scripting language does not need to be overloaded because the type of the argument itself is not deterministic, and since the type can be arbitrary, there is no point in improving performance. For example, the Sub F (a) of Python: Any type can be used to Fdef Max (**kwargs). Because Python is a dynamic language, why not?

def max (a,b,c= ' nothing '):

If c== ' nothing ':

If a > B:

Print a

Else

Print B

Else


......
Is this more complicated than overloading?
  • 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.