Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the parameters of python function

2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use the parameters of the python function". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to use the parameters of the python function.

Preface

Python parameter types and parameters are passed in a variety of forms, and it is more flexible to use python than other languages. It is generally divided into two forms of parameter transfer: position parameter transfer and keyword parameter transfer. The parameter-passing form is used when calling a function relative to the argument. Generally speaking, there are two types of parameters: default parameters and variable length parameters. The parameter type is defined when the function is created, relative to the formal parameter.

Position parameter transfer

Position parameter transfer, a form of function parameter transfer that we often use, which is passed to the form parameter of the corresponding position according to the position of the argument. When mixed with other forms of parameter transfer, the position parameters must be input first.

Def func (a, b, c): print (a, b, c) # position parameter if _ _ name__ ='_ _ main__': func (1, 6, 7) # print result is 1 67 position one to one corresponding func (6, 1, 7) # print result is 6 17 # func (6, 1) # the number of real participating parameters must be consistent # func (6, 1, 7) 9) # neither more nor less keyword parameters

The keyword passes parameters, and the corresponding assignment is made according to the name of the formal parameter.

Def func (a, b, c): print (a, b, c) # keyword parameter if _ _ name__ = ='_ _ main__': func (1, bread6, cantilever 7) # print result is 1 67 func (cantilever 3, bread1, Apocalypse 7) # printout result is 7 1 3 # func (cymbals 2, 3) # No less assignment # func (cymbals 2, baccalaureate 3 flags 6 flags 5) # No more assignments # func (cymbals 2, bread3) The assignment of # func (1, baked 6,7) # cannot be irrelevant when mixed with position passing parameters. The position parameter must come first, and the default parameter cannot be used alternately.

The default parameter is that the parameter will have a default value. If a new value is passed in, the new value will be used, otherwise the default value will be used. Parameters also need to pay attention to the position when using the default parameters, which must be written after the position parameters (in fact, they cannot be called position parameters, but the ordinary parameters that are not assigned are called position parameters for understanding), otherwise an error will be reported. The ab below must be written after c.

# default parameters must be written on the right side of def func (a, bread5, canti3): print (a, b, c) # default parameters if _ _ name__ = ='_ main__': func (1) # the printed result is 1 53 func (1, 6) # the printed result is 1 63 func (1, 2, 7) # the printed result is 1 27 func (bread1 Await 4) # print result is 4 1 3 indefinite length parameter

There are two forms of indefinite length parameters.

Add a * (* args) in front of the parameter to indicate an indefinite length tuple parameter, which can receive all the arguments passed through the position and store them in the tuple.

Add two * (* * kwargs) in front of the parameter to indicate the indefinite length dictionary parameter. You can receive all the arguments passed through the keyword and store them in the dictionary.

# variable length parameter def func (* args, * * kwargs): print (args) print (kwargs) if _ name__ = ='_ _ main__': func (1, 4, 8, 7, axi5, hog6, lumb15) # pay attention to the corresponding order of actual parameters

Result

(1,4,8,7) # tuple

{'averse: 5,' hobby: 6, 'lump: 15} # Dictionary

Def func1 (a, b, args, c, kwargs): print (a, b, args, c, kwargs) when creating mixed-use questions

Let's first give them a name for easy reference. Call an a the normal parameter, b the default parameter, * args the variable parameter (because it can receive multiple arguments), and * * kwargs the variable keyword parameter (because it can receive multiple arguments passed through the keyword).

Ordinary parameters must be written first (before other types of parameters)

Variable keyword parameters must be written at the end (after other parameter types)

Variable parameters and variable keyword parameters can only appear once.

When quoting

Position parameters must be written in front of keyword parameters, and they cannot be used interchangeably. When is the right time for keyword parameters to appear? Appears after a variable parameter, which is equivalent to a termination condition.

Def func1 (a, baked 5, * args, cantilever 7, * * kwargs): print (a, b, args, c, kwargs, sep='\ n') if _ _ name__ = ='_ _ main__': func1 (5, 6, 8, 7, 1, 5, 6, 8, canti4, rDNA 2, spore 3)

The result is

five

six

(8, 7, 1, 5, 6, 8)

four

{'ringing: 2,' s giving: 3}

Thank you for your reading, the above is the content of "how to use the parameters of the python function". After the study of this article, I believe you have a deeper understanding of how to use the parameters of the python function. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report