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 correctly use function comments in Python

2025-03-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article focuses on "how to correctly use the function annotations in Python", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "how to correctly use the function comments in Python"!

When I looked at the fixture source code of python, I found that the method definition of fixture is as follows:

Def fixture (fixture_function: Optional [_ FixtureFunction] = None, *, scope: "Union [_ Scope, Callable [[str, Config], _ Scope]]" = "function", params: optionally [Iterable] = None, autouse: bool = False, ids: Optional [Union [Iterable [Union [None, str, float, int, bool]], Callable [[Any], Optional [object]] ] = None, name: Optional [str] = None,)-> Union [FixtureFunctionMarker, _ FixtureFunction]:

I am suddenly a little messy, do not know what this is, after a variety of online search information, found that this is a new feature of Python 3.x, called function annotation Function Annotations. Its purpose is not a rigid requirement at the syntax level, but as the name implies, it can be used as an additional comment to the function. His usage is also very simple.

Define normal functions in python as follows:

Def F1 (aformab): return aforb

Through function comments, the method is defined as follows:

Def f2 (a: "str type parameter a", b: "str type parameter b")-> str: print ("Annotations:", f2.roomannotationsparameters _) return aquib

Among them

A: "str type parameter a" represents the description of parameter a

B: "str type parameter b" represents the description of parameter b

-> str: represents the return value of the function

F2. Comment annotationscomment _ view the comment description of the function

Run print (f2 ('aa','bb')) and output:

Annotations: {'averse:' str type parameter await, 'baked:' str type parameter baked, 'return':}

Aabb

So if we can't modify the parameter type and return value type of the function, let's try the following:

Print (f2 (1pyr2)), output:

Annotations: {'averse:' str type parameter await, 'baked:' str type parameter baked, 'return':}

As you can see, Function Annotations is only used to annotate the function, and cannot specify the parameter type.

At this point, I believe you have a deeper understanding of "how to correctly use the function comments in Python". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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