Example Analysis of python Keyword Passing Parameters

  • 2021-11-10 10:01:40
  • OfStack

1. Description

Keyword passing participates in argument association in the form of "parameter variable name = argument", and passes parameters according to the name of parameter, so that the order of argument and parameter is different. Don't worry about the order of parameters when defining functions, just specify the corresponding names when passing parameters.

2. Two forms


makeup_url(protocal='http', address='www.baidu.com')
makeup_url(address='www.baidu.com',protocal='http')

3. Examples


def makeup_url(protocal, address):
print("URL = {}: //{}".format(protocal, address))

Content extension:

python-Keyword passing parameters

1. Must be passed by keyword

Variables after * must be passed by keyword

eg:

def kwonly (a, * b, c): # c must be passed by key, b receives the remaining parameters, a can be passed by position or by key

kwonly (1, 2. c = 3) Correct

kwonly (1, 2, 3, c = 4) Correct

kwonly (1, 2, 3) Error

def kwonly (a, *, b, c) # a can be transferred according to position or parameters. b and c must be transferred according to parameters, and redundant parameters are not allowed

kwonly (1, b = 2, c = 3) Correct

kwonly (a = 1, b = 2, c = 3) Correct

kwonly (c = 1, a = 2, b = 3) Correct

kwonly (1, 2, 3) Error

2. Default values can appear for keyword passing. Parameters passed by keywords with default values can be passed without parameters

3. You can't have two *


Related articles: