Why you don't need to reload in Python

Source: Internet
Author: User

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.

OK, so for the case (1), the function is 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 the case (2), the function is the same, but the number of parameters is different, how does python handle it? 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 the situation (1) and the scenario (2), Python naturally does not need function overloading.

Here, by the way, when the parameters are passed in Python *arg and **args, see a few small chestnuts are clear:

First Little chestnut:

1 defTest1 (farg,*args):2     Print("Farg:", Farg)3      forValueinchargs:4         Print("args:", value)5 6Test1 (1," Both", 3,"4")

Attached results:

The second little chestnut:

1 def test2 (farg,**args):2     print ("farg:", Farg)3for in       args:4         print ( " key:%s  values:%s"% (Key,args[key]))

Results:

The following example is clearer, defining the role of * when you assemble a narimoto group of positional arguments,* * to assemble the keyword arguments into a dictionary

1 def test1 (x, y, z, *args):  2     print  (x, y, Z, args)  3  4 5 def test2 (x, y, z, * *Kwargs):  6     print  (x, Y, Z, Kwargs) 7 8  9 test1 (1, 2, 3, 4, 5, 6, 7, 8, 9,, one, one, ten,) , Test2 (1, 2, 3, A=4, b=5)

The following example is called when the role of the * is to break the tuple or list into positional parameters for parameter passing, * * The role of Breaking the dictionary into keyword parameters for parameter passing

Below

1 deftest3 (x, Y, z):2     Print(x, Y, z)3 4x = [1, 2, 3]5X1 = ('Q','W','e')6y = {'x': 1,'y': 2,'Z': 3}7TEST3 (*x1)8TEST3 (*x)9Test3 (**y)

Why you don't need to reload in Python

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.