SoFunction
Updated on 2024-12-20

Example analysis of python keyword passing parameter

1. Description

Keyword passing participates in real parameter association in the form of "formal parameter variable name = real parameter", passing parameters according to the name of the formal parameter, so that the order of real and formal parameters is not consistent. You don't need to worry about the order of parameters when defining a function, you can just specify the corresponding names when passing parameters.

2. Two forms

makeup_url(protocal='http', address='')
makeup_url(address='',protocal='http')

3. Examples

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

Content Extension:

python-keyword-passing-arguments

1. Must be passed in accordance with the keywords

Variables after * must be passed in accordance with the keyword

eg:

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

kwonly(1, = 3) is correct

kwonly(1,2,3,c = 4) is correct

kwonly(1,2,3) error

def kwonly(a,*,b,c)#a can be passed by position or by argument, b,c must be passed by argument, and no extra arguments are allowed.

kwonly(1,b = 2,c = 3) is correct

kwonly(a = 1,b = 2,c = 3) is correct

kwonly(c= 1,a = 2,b = 3) is correct

kwonly(1,2,3) error

2. Keyword passing can appear as a default value. And there is a default value of the keyword passed parameters can not pass parameters

3. No two * may appear

to this article on the python keyword passing parameter example analysis of the article is introduced to this, more related python keywords how to pass parameters content please search my previous articles or continue to browse the following related articles I hope you will support me in the future more!