Parameter-parameter combination of the day5 - 07 function of Python study notes

Now we have learned five parameter types: positional parameters, default parameters, variable parameters, keyword parameters, and named keyword parameters.
Functions are defined in Python, which can be combined using these five parameters.
But the order of parameter definitions must be: required parameters, default parameters, variable parameters, named keyword parameters and keyword parameters
            def f1(a, b, c=0, *pp, **kw):
                print(a,b,c,pp,kw)
            dd = [9,10,12,14,15,17,19,20]
            dk = {'q':99, 'p':88}
            f1(*dd,**dk)
            9 10 12 (14, 15, 17, 19, 20)
            def f2(a,b,c=0,*,job,hh):
                print(a,b,c,job,hh)
            dw = (1,2,3)
            ww ={'job':'jj','hh':'HH'}
            f2(*dw,**ww)
            1 2 3 jj HH
Functions can be called with a tuple and a dict.
            def f3(a,b,c,*pp,host,words,**kw):
                print(a,b,c,pp,host,words,kw)
            pp1 = (1,2,3,4,5, 6,7,8,9,0)
            pp2 = {'host':'HH', 'words':'WW'}
            pp3 = {'oo':'oooo', 'ppp':'pppppp'} pp4
            = {'host':'HH', 'words':'WW', 'oo':'oooo', 'ppp':'pppppp'}
            f3(*pp1,**pp2,**pp3)
            f3(*pp1 ,**pp4)
            1 2 3 (4, 5, 6, 7, 8, 9, 0) HH WW {'oo': 'oooo', 'ppp': 'pppppp'}
            1 2 3 (4, 5, 6, 7, 8 , 9, 0) HH WW {'oo': 'oooo', 'ppp': 'pppppp'}
Actually, for any function, it can be called by something like func(*args, **kw), regardless of How its parameters are defined.
Although you can combine up to 5 parameters and, but do not use too many combinations at the same time, otherwise the readability and understandability of the functional interface will be very poor.

Exercise:
The following function allows to calculate the product of two numbers, please modify it slightly to accept one or more numbers and calculate the product:
def product(x, y):
    return x * y
-------------------------------------
def pro(*x,y=1):
     s = 1
     for i in x:
             s = i * s
     print( s*y )

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325237700&siteId=291194637