Easy Tutorial
❮ Mac Scala Chinese Show Php Get Url Suffix ❯

The Difference Between a Single Asterisk () and a Double Asterisk (*) in Python Function Arguments

Classification Programming Techniques

def foo(param1, *param2):
def bar(param1, **param2):

Single Asterisk (*): *args

Import all parameters as a tuple:

Example

def foo(param1, *param2):
    print (param1)
    print (param2)
foo(1,2,3,4,5)

The output of the above code is:

1
(2, 3, 4, 5)

Double Asterisk (*): *kwargs

The double asterisk (**) imports parameters as a dictionary:

Example

def bar(param1, **param2):
    print (param1)
    print (param2)
bar(1,a=2,b=3)

The output of the above code is:

1
{'a': 2, 'b': 3}

Additionally, another use of the single asterisk is to unpack argument lists:

Example

def foo(tutorialpro_1, tutorialpro_2):
    print(tutorialpro_1, tutorialpro_2)
l = [1, 2]
foo(*l)

The output of the above code is:

1 2

Example

def foo(a, b=10, *args, **kwargs):
    print (a)
    print (b)
    print (args)
    print (kwargs)
foo(1, 2, 3, 4, e=5, f=6, g=7)

The output of the above code is:

1
2
(3, 4)
{'e': 5, 'f': 6, 'g': 7}

>

Original article link: https://www.cnblogs.com/arkenstone/p/5695161.html

** Click to Share Notes

Cancel

-

-

-

❮ Mac Scala Chinese Show Php Get Url Suffix ❯