* ** the role and

  • In front of python difference function parameters single asterisk (*) and double asterisk (**) in

    • * The arbitrary number of arguments passed to the function tuple

      >>> def foo(param1, *param2):
              print param1
              print param2
      >>> foo(1,2,3,4,5)
      1
      (2, 3, 4, 5)
    • In addition, another usage single asterisk are extracting the parameter list:

      >>> def foo(bar, lee):
              print bar, lee
      >>> l = [1, 2]
      >>> foo(*l)
      1 2
    • Double asterisk ( ): kwargs
      parameters in the dictionary introduced in the form of

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

Guess you like

Origin www.cnblogs.com/rise0111/p/11359497.html