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

Why there is no void keyword in Python

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the knowledge of "Why there is no void keyword in 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!

Simply put, void is a type (type), but there is no specific value (value).

What on earth does this mean?

Taking several common types of Python as an example, we can see the rule from the comparison: int is a type representing integers with infinite possible integer values; bool is a Boolean type with two possible values (True and False); and NoneType is a type representing None with only one value (None).

As for void, it is a more abstract special type, but does not contain any values.

After introducing the conceptual meaning, we can get down to business. The question in the title can be further broken down into two:

Why do other languages use the void keyword?

Why didn't Python design the void keyword?

For the first question, let's take a look at the two usage scenarios of void (PS: only the use of functions is considered here, not the use of pointers, because Python does not have pointers):

When void is used in the argument position of a function, it indicates that the function does not need to pass parameters.

At first, f () in C language means that the number of parameters is uncertain. In order to express the semantics of "no parameters", f (void) is introduced as a limitation. Later languages (including Python) basically did not use void in the parameters, but directly used f () to indicate that there was no need to pass parameters. C++ supports both grammars in order to be compatible with C.

When void is used as a modifier before a function, it indicates that the function does not return a value.

In C, if the return type is not declared, the f () function returns the value of the integer after compilation. To avoid confusion, use void f () to qualify when a return value is not needed.

At the same time, more importantly, it also acts as a placeholder, indicating that the type of a function is known, which is helpful for code readability and compilation.

Void is the null return value type of the function, and this usage is also inherited in C++/Java. In addition, void is also present in Javascript, but it becomes an operator and plays a completely different role, which is not shown here.

However, Python does not have the void keyword from beginning to end.

Why is this? Is it because there are no problems faced by other languages in Python? Or does Python have its own set of solutions?

Let's take the two uses related to functions as examples.

When the function does not need to pass parameters, the writing of f (void) is superfluous at all, so Python uses the simplest and most straightforward no-parameter method f ().

As for the use of return value types, when we define a function, such as the simplest def func (): pass, in order for its call result func () to be a valid object, it must have a valid type (type).

This should be a common problem encountered by type-based programming languages, and Python is no exception.

At this point, if the function itself does not explicitly return an object, there are two possible solutions:

Method one, that is, declare the function to be of type void, as C and other languages do, as long as it passes the type checking.

The second is the method used by Python, that is, to make the interpreter implicitly return a None object, that is, to make the function get a NoneType type by default, and then use it for type checking (PS:Javascript is similar, except that it returns undefined by default, which is not an object, but a type that represents "undefined", similar to void).

To put it simply, the design idea of Python is to reuse existing NoneType types directly and let the interpreter fill the missing function types.

With regard to the implicit filling process of the Python interpreter, I have analyzed it in detail in the last article "Why does the Python function return None by default?" which can be found by interested students.

This has at least two advantages: no new void types and keywords are introduced, and there is no need for the programmer to declare the return type before the function, which is consistent with the explicit return value.

Just imagine, if Python doesn't let the function have a return value by default, it might be written as void def func ():. In this form, it becomes a special case of function definition. Compared with another special case function, the asynchronous function asyc def func ():..., it can cause confusion.

Overall, Python seems to think that the void null type is not so necessary, it seems that the NoneType type is sufficient, and when the return value is missing, it is extremely convenient to have the interpreter uniformly inject, which is why we see the status quo.

This is the end of the content of "Why Python does not have void keyword". 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.

Share To

Development

Wechat

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

12
Report