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 analyze only position parameters in Python3.8

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

How to analyze the only location parameters in Python3.8, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.

Only positional parameters (Positional-Only Arguments) of the new Python3.8 feature

Functions pass parameters in a variety of ways, including position parameters, default parameters, variable parameters, keyword parameters, and named keyword parameters. For example:

Def add (x, y, * args, * * kwargs):

Print (f "x = {x}, y = {y}")

Where x and y are two positional parameters, we can call

> > add (1,2)

Xylene 1, yellow2

Because it is a position parameter, strictly abide by the position order.

However, you can also pass x and y as named keyword parameters, and the order of the parameters can be changed, such as putting y in front without affecting the result.

> add (yearly 2, xylene 1)

Xylene 1, yellow2

The second approach seems to be more flexible, and the risk of error increases, especially in multi-person projects.

So how to prohibit such calls at the syntax level to avoid errors?

Here, you can use the position-only syntax in Python3.8. When defining a function, you can specify a slash (/) between the parameters. The parameters before the slash strictly follow the definition of position-only parameters, for example:

> def add (x, y, /, * args, * * kwargs):

... Print (f "x = {x}, y = {y}")

...

> add (1Pol 2)

Xylene 1, yellow2

"/" tells the interpreter that x and y are two strict positional parameters that cannot be passed as named keyword parameters. What if you pass a parameter as a named keyword parameter?

> add (yearly 2, xylene 1)

Traceback (most recent call last):

File "", line 1, in

TypeError: add () missing 2 required positional arguments:'x 'and'

Reported an error, the system does not allow you to use this way to call, only supports the location of the parameter to call, the call can not specify the name of the parameter.

In fact, this syntax has appeared in Python3.7, such as the built-in function float

Python3.6 can be called by specifying the parameter name.

> float (Xero1)

1.0

Python3.7 will report an error.

> float (Xero1)

Traceback (most recent call last):

File "", line 1, in

TypeError: float () takes no keyword arguments

> > float (1)

1.0

You can take a look at the help documentation for float under python3.7.

> help (float)

Help on class float in module builtins:

Class float (object)

| | float (xchang0, /) |

| |

| Convert a string or number to a floating point number, if possible.

| |

| Methods defined here:

| |

| | _ abs__ (self, /) |

| | abs (self) |

You will find that there is also a "/" in the float definition, indicating that x is the location parameter, and the name cannot be specified when called.

Summary

Only the syntax of the position parameter is that when the function is defined, the position parameter is separated by "/", and the named keyword parameter is not allowed to be called when calling. Do you understand?

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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

Internet Technology

Wechat

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

12
Report