Summary of parameters knowledge for Python functions

Source: Internet
Author: User

function is not ripe, behind the dizzy, here re-induction of Liao function tutorial, deepen memory

The parameters of a function are divided into:

  1. Position parameters
    1. 1 def Power (x):           # x is a positional parameter of power (x), we call power (x) must pass in a parameter x2     return x * x

  2. Default parameters
    1. 1 def Power (x, n=2):  the #修改后的power () function has two positional parameters, and when the function is called, if two parameters are passed, it is passed to x,n in turn; 2     s = 1        #如果仅仅传入一个参数的话, then n=2 is a default parameter, default is 2, which is the default parameter 3      while n > 0:4 C13/>n = n-15         s = S * x6     return s

      1) using the default parameters, can reduce the difficulty of calling the function;
      2) to set the default parameters, to follow the necessary parameters before, the default parameters after the principle;
      3) When multiple parameters are set, the parameters with frequent changes are placed in front, and the change frequency can be lowered to the back as the default parameters;
      4) Define default parameters to keep in mind, the default parameter must point to the immutable object.

  3. Variable parameters
    1. 1 def Calc (*numbers):2     sum = 03      for in Numbers:4         sum = SUM + n * n5     return sum

      1) Define a variable parameter, you need to add an * number before the parameter, the variable parameter inside the function receives a tuple, in the call function can pass any parameter;
      2) If the parameter itself is a list or a tuple, call the function should be in the list or a tuple before adding a *, the list/tuple into a variable parameter passed into;

      1 >>> nums = [1, 2, 3]2 >>> Calc (*nums)    #calc (*[1,2,3]) is also possible 3 14

  4. Keyword parameters
    1. 1 defPerson (name, age, * *kw):2     Print('Name:', Name,'Age :', Age,'Other :', kw)3 4 5>>> person ('Bob', city='Beijing')6Name:bob age:35 Other: {' City':'Beijing'}7>>> person ('Adam', gender=,'M', job='Engineer')8Name:adam age:45 Other: {'Gender':'M','Job':'Engineer'}

      Keyword parameter run you pass in 0 or more parameters with parameter names, these parameters with parameter names are automatically encapsulated into a dict within the function;
      Of course, you can convert a dict into a keyword parameter (plus * * in front of the dictionary) to pass in the function;

    2. >>> extra = {' City':'Beijing','Job':'Engineer'}>>> person ('Jack', city=extra[' City'], job=extra['Job']) Name:jack Age:Other: {' City':'Beijing','Job':'Engineer'}>>> extra = {' City':'Beijing','Job':'Engineer'}>>> person ('Jack', 24, * *extra) Name:jack Age:Other: {' City':'Beijing','Job':'Engineer'}
  5. Named keyword parameters
    1. If you want to control the incoming of keyword parameters, you can review by KW:
      defPerson (name, age, * *kw):if ' City' inchKW:Pass #can be replaced by a check statement    if 'Job' inchKW:Pass #can be replaced by a check statement    Print('Name:', Name,'Age :', Age,'Other :', kw)

      But callers can still pass in unrestricted keyword arguments

    2. If you want to restrict the name of a keyword argument, you can use the named keyword argument, for example, to receive city and job act as a keyword parameter. The functions defined in this way are as follows:
      def person (name, age, *, City, Job):    Print(name, age, city, job)

      Unlike keyword Parameters **kw , a named keyword parameter requires a special delimiter * , and * subsequent arguments are treated as named keyword parameters.
      The calling method is as follows:

    3. >>> person ('Jack', city='Beijing', job='Engineer'Beijing Engineer
    4. If there is already a mutable parameter in the function definition, then the named keyword argument that follows will no longer need a special delimiter * :
      def person (name, age, *args, City, job):    Print(name, age, args, city, job)

    5. The named keyword argument must pass in the parameter name, which differs from the positional parameter. If the parameter name is not passed in, the call will error.

      A named keyword parameter can have a default value, which simplifies the invocation:

        1. def person (name, age, *, city='Beijing', Job):    Print (name, age, city, job)

Two, parameter combination:

The order of the parameter definitions must be: required, default, variable, named keyword, and keyword parameters.

At the time of the function call, the Python interpreter automatically passes the corresponding parameters according to the parameter location and argument name.

  1. defF1 (A, B, c=0, *args, * *kw):Print('A ='A'B ='B'C ='C'args =', args,'kw =', kw)defF2 (A, B, c=0, *, D, * *kw):Print('A ='A'B ='B'C ='C'd ='D'kw =', kw)>>> F1 (1, 2) A= 1 b = 2 c = 0 args = () kw = {}>>> F1 (1, 2, c=3) A= 1 b = 2 c = 3 args = () kw = {}>>> F1 (1, 2, 3,'a','b') A= 1 b = 2 c = 3 args = ('a','b') kw = {}>>> F1 (1, 2, 3,'a','b', x=99) A= 1 b = 2 c = 3 args = ('a','b') kw = {'x': 99}>>> F2 (1, 2, d=99, ext=None) A= 1 b = 2 c = 0 d = HP = {'ext': None}>>> args = (1, 2, 3, 4)>>> kw = {'D': 99,'x':'#'}>>> F1 (*args, * *kw) a= 1 b = 2 c = 3 args = (4,) kw = {'D': 99,'x':'#'}>>> args = (1, 2, 3)>>> kw = {'D': 88,'x':'#'}>>> F2 (*args, * *kw) a= 1 b = 2 c = 3 D ='x':'#'}

Summary of parameters knowledge for Python functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.