Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the five types of parameters in python

Shulou Source: shulou.com Published: 2022-06-01 03:49:27 09月24日 Update

This article mainly introduces what are the five types of parameters in python. It is very detailed and has a certain reference value. Friends who are interested must read it!

Examples of the use of five types of parameters of function

Python five types of parameters: position parameters, keyword parameters, default parameters, variable position and keyword parameters

Def f (print: {a}, b: {b}, c: {c}, d: {d}') 12

The default parameter c cannot be after the variable keyword parameter d.

Call f:

F: {'width': 10,' height': 20}: {'Magne 10,' Widthpieces 10, etc.) a 1, (2, 5), cpart 10, d: {'

The variable position parameter b argument is parsed into tuples (2J5), while c gets the default value of 10; d is parsed as a dictionary.

Call f again:

F (aplomb: ()): (), ()

An is the keyword parameter when an is passed into aqum1, but no value is passed for b _ # d, and c is passed into 12 instead of the default value.

Note that the parameter a can be either f (1) or f (axi1), which is more readable than the first one. It is recommended to use f (aqui1). If you want to force the use of f (axi1), you need to add an asterisk before it:

Def f (*, aformaitb): print (favoa: {a}, b: {b}')

If f (1) is called at this point, an error will be reported: TypeError: F () takes 0 positional arguments but 1 was given

Only f (axi1) can OK.

It shows that the previous * works, and it can only pass in keyword parameters, so how do you check the type of this parameter? With the help of python's inspect module:

In [22]: for name,val in signature (f) .parameters.items ():...: print (name,val.kind)...: a KEYWORD_ONLYb VAR_KEYWORD

You can see that the parameter an is of type KEYWORD_ONLY, that is, just a keyword parameter.

However, if f is defined as:

Def f (a quotation b): print (favoa: {a}, b: {b}')

View parameter types:

In [24]: for name,val in signature (f) .parameters.items ():...: print (name,val.kind)...: a POSITIONAL_OR_KEYWORDb VAR_POSITIONAL

You can see that the parameter a can be either a position parameter or a keyword parameter.

These are all the contents of the article "what are the five parameters in python?" Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Parameter key keyword location type variable content article that is value function example interest again function readability dictionary boy buddy that is Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Linux Shulou Tech Info Docker MariaDB