current position:Home>Python: parameter types

Python: parameter types

2022-02-02 14:14:59 Ice sky

  • Shape parameter : Parameter name when defining a function

  • Actual parameters : Parameters passed during function call

  • Positional arguments : From left to right

  • Key parameters : When a function is called , Assign a value to the specified formal parameter ex. func(para1=1, para2=2, ...), The specified values of all formal parameters must be given

  • Default parameters : Set default values for formal parameters when defining functions ex. def func(age=20, name='somebody')

  • Variable parameters :

    • *args: Quantity delivered on demand , Function execution doesn't care about the amount of data , for example

    • def count(*data):
          return len(data)
      
      # call count
      count(1, 2, 3)
      

      here data Will be stored in a tuple (1, 2, 3), The return result is 3

    • **kwarg: Empathy , Here are variable keyword parameters , namely func(kw1=val1, kw2=val2,...)

  • Unpacking of variable parameters :

    • args = (1, 2, 3)
      
      def func(para1, para2, para3):
          # do something
          pass
      
      # using * to extract arguments
      func(*args)
      
      # if dictionary
      d = {
              'a': 1, 'b': 2, 'c': 3}
      # require number of keys matched up with function's list of parameters
      func(**d)
      
  • Parameter sequence : Avoid ambiguity during compilation

copyright notice
author[Ice sky],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/02/202202021414571082.html

Random recommended