5.3Python function (3): indefinite length position parameter & indefinite length keyword parameter

Keywords: Python less

@Variable length position parameter

  • Use the location parameter defined by * arg_name to represent any multiple location parameters;
  • In Python standard library, * args is used to name indefinite length position parameter. Of course, we can customize this name;
  • The type of indefinite length position parameter is tuple;

In the following example, the * likesBy, which means the person who likes, is obviously uncertain

def sayLove(sender,toWhom,times,*likesBy,city="Shenzhen",date="8 7 June"):
    print("Come from%s The affectionate expression of"%(sender))
    print("Dear%s: "%(toWhom))

    for i in range(times):
        print("I'm going to sleep with you through most of China")
        print("I'm sleeping through the gunfire")
        print("I press countless nights into a dawn to sleep with you")
        print("I'm a million I'm running into one I'm going to sleep with you")

    #Apply key parameters
    print("-%s to %s"%(date,city))

    # Apply variable length position parameters
    print(likesBy,"Praise points")

    # Use [return value] to return the result to the caller. The return value must be in the last step
    return "Oh"
    # return 666
    # return False

Make a call to this function, passing three admirers

result = sayLove("Lin Ai Hua","LaFang",1,"Yi Tian","Er Kang","Shan Xin",city="Beijing",date="1 1 June")
print("The reply is:",result)

The results are as follows

@Indefinite length key parameter

  • Use the keyword parameter defined by * * arg_name to represent any multiple keyword parameters;
  • In Python standard library, we are accustomed to using * * kwargs to name variable length location parameters. We can customize this name;
  • The type of indefinite length keyword parameter is dictionary;

In the following example, * ps indicates the remark information. Obviously, the remark information can be more or less

def sayLove(sender, toWhom, count, *args, city="Shenzhen", date="8 7 June", **ps):
    print("Come from%s The affectionate expression of" % (sender))
    print("Dear%s: " % (toWhom))

    for i in range(count):
        print("I'm going to sleep with you through most of China")
        print("I'm sleeping through the gunfire")
        print("I press countless nights into a dawn to sleep with you")
        print("I'm a million I'm running into one I'm going to sleep with you")

    # Apply variable length position parameters
    print(args, "Praise points")

    # Apply variable length key parameter
    print("Note information is",ps)

    # Use [return value] to return the result to the caller. The return value must be in the last step
    return "Oh"

Call the above method to pass in a set of custom key value pairs at all indefinite key parameters

result = sayLove(
    "Lin Ai Hua", "LaFang", 1,  # Fixed length position parameter
    "Yi Tian", "Er Kang", "Shan Xin",  # Variable length position parameter
    date="1 1 June", city="Taipei",  # Fixed length key parameter
    breakfast="Goubuli bun", lunch="Braised chicken and rice", supper="A bowl of fragrant cask rice"  # Indefinite length key parameter
)
print("The reply is:", result)

The results are as follows

Posted by Destramic on Wed, 01 Apr 2020 12:59:59 -0700