Python's position parameter, the difference between the default parameters, keyword parameters, variable parameter

Python's position parameter, the difference between the default parameters, keyword parameters, variable parameter

1. Location parameter

  • The parameters required to define the position of the reference transfer function is called
  • DEF print_hello (name, Sex):
         Print ( ' the Hello% S% S, is available for purchase to Python world! ' % (name, ' Mr. ' )) 
    print_hello ( ' Nick ' , ' sir ' )
     # Output: Mr. hello Nick, welcome to python world!

   Location parameters must be in strict accordance with the order parameter passing

2. Keyword parameters

  • In the function call, the "key-on" form as a function parameter, not in accordance with the parameters passed to the function position
  • note:
    • → key parameters must be in the right position parameters
    • For the same parameter value can not be repeated passages
  • # In order as a function of the parameter passed by value 
    DEF MSG (name, Age, Sex):
         Print ( " the Hello, your name IS% S, Age IS% S, Sex IS% S " % (name, Age, Sex)) 
    MSG ( ' Nick ' , 23 is, ' MALE ' )
     # outputs: the Hello, Nick your name IS, IS 23 is Age, Sex MALE IS 
    
    # when positional parameters and keyword parameters, the position of the keyword parameter argument following 
    MSG ( ' Tony ' , 12 is, Sex = ' MALE ' ) 
    
    # parameter multiple keywords parameters, regardless of the order parameter keyword 
    MSG (Sex = ' FEMALE ' , name = ' Amy', age=56)

     

3. Default parameters

  • When defining the function, you can provide default values ​​for shape parameters. For parameter has a default value, if the function is called, the value for the parameter passed in by value, otherwise the default value
  • note:
    • When calling the function may not have to pass the parameter value defaults
    • Whether defined functions or calls, default parameters should be defined formal parameters in the right position
    • Only once when defining assignment
    • Default parameters should generally be defined as immutable type

 

Guess you like

Origin www.cnblogs.com/swordsman180309/p/11802011.html