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

What is a strong type of Python imitation?

2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains "what is the strong type of Python imitation". 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 "what is the strong type of Python imitation".

Environment Python interpreter 3.6 +

With regard to the Python version, choose Python3.6+, as much as possible because after Python3.6+, the asynchronous rise of Python is complete, although it is currently in the testing stage, but I believe that before long, Python will be better.

A simple example def speak (name,age): print (name,age) speak (Zhang San, 18)

We can clearly know that the name parameter of the speak function must receive a string, and in the future I hope it will always receive a string, the traditional way of writing is like the above, of course, now I also write this, after all, I am too bad.

But now there is a very simple requirement, I want to convert all the parameters received by name to uppercase, so, we all know that strings have upper methods.

Print ("stark" .upper ())

But when we hit "." Sometimes there may not be an automatic prompt for symbols, but name is really a string.

Note: Pycharm may prompt, but sometimes it fails, thanks to the powerful prompting capabilities of Pycharm, but not everything, as shown in the following figure.

This is normal because Python is a dynamic language, and the type of variable is not known until it is explained.

Let's change the code a little bit, as shown in the following figure.

We add: str after the parameter, which means that I want the parameter type I receive to be a string, if I pass a numeric type, as shown in the following figure.

A yellowing warning appears in Pycharm or Vscode, as shown in the following figure.

The expected type 'str',' is' int''.

And when typing the dot, as long as it is a mainstream editor, it will be prompted automatically, as shown in the following figure.

Declare simple types

Several commonly used

Whether it is a function argument or a declared variable, it is: type

More types of Lists

Dicts

Class

The effect of + and not + type comments

The following figure shows the effect of + type comments:

The following figure shows the effect of not + type comments:

Function

Thank you for your reading, the above is the content of "what is the strong type of Python imitation". After the study of this article, I believe you have a deeper understanding of what the strong type of Python imitation is, and the specific use needs to be verified in practice. 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

Internet Technology

Wechat

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

12
Report