In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how to add meta-information to function parameters by python". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1. Method
Using function argument annotations is a good way to prompt programmers how to use this function correctly. For example, here is an annotated function:
Def add (x:int, y:int)-> int: return x + y
The python interpreter does not add any semantics to these annotations. They are not checked by type, and the runtime is no different from what it was before it was annotated. However, it is very helpful for those who read the source code. Third-party tools and frameworks may add semantics to these annotations. At the same time, they also appear in the document.
> > help (add) Help on function add in module _ main__:add (x: int, y: int)-> int >
Although you can use any type of object to annotate a function (such as numbers, strings, object instances, etc.), it is generally better to use classes or strings.
two。 Supplement
Function annotations are only stored in the _ _ annotations__ attribute of the function. For example:
> add.__annotations__ {'yearly:,' return':, 'Xerox:}
Although annotations may be used in many ways, their main use is documentation. Because python does not have a type declaration, it is usually difficult to know what parameters should be passed to this function just by reading the source code. At this point, the use of annotations can give programmers more hints so that they can use functions correctly.
This is the end of the content of "how to add meta information to function parameters by python". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.