程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Why doesnt Python have a void keyword?

編輯:Python

void Is one of the most common keywords in programming languages , Literally , It is “ Empty 、 An empty set 、 blank ” It means , A return value type most commonly used to represent functions .

There's a definition on Wikipedia :

The void type, in several programming languages derived from C and Algol68, is the type for the result of a function that returns normally, but does not provide a result value to its caller.
stay C、Algol68 And their derived programming languages ,void A type is a type that a function returns normally , But it doesn't return a value to the caller .

Simply speaking ,void It's a type of (type), But there is no specific value (value).

What exactly does this mean ?

With Python For example, several common types of , We can see the law from the contrast :int Is a type of integer , It has infinite possible integer values ;bool It's a boolean type , It has two possible values (True and False);NoneType It's a sign of None The type of , It has only one value (None).

as for void, It's a more abstract special type , But it doesn't contain any values .

After introducing the concept , We can get to the point . The question in the title can be further broken down into two :

  • Why do other languages use void keyword ?
  • Python Why not design void keyword ?

For the first question , We use C/C++ For example , Have a look first void Two usage scenarios of

When void When used in the parameter position of a function , It means that the function does not need to pass parameters .

first C Linguistic f() Indicates that the number of parameters is uncertain , In order to express “ No parameters required ” The semantics of the , So the introduction of f(void) As a limit . Later language ( Include Python) Basically not used in parameters void, It's direct use f() No need to pass the reference .C++ For compatibility C, That's why they support both grammars .

When void When used in front of a function , It means that the function has no return value .

stay C In language , If you do not declare the return type , be f() The function will return the integer value after compilation . To avoid chaos , When you don't need a return value , Just use void f() To limit .

meanwhile , More importantly , It also acts as a place holder , Indicates that the type of a function is known , This is helpful for code readability and compilation .

void As the null return value type of the function , This is used in C++/Java And it's been inherited from . in addition , stay Javascript There are also void The figure of , It's just that it becomes an operator , It plays a completely different role , Not here .

however ,Python From the beginning to the end void keyword .

Why is that? ? Is it because of Python Isn't there a problem with other languages ? Still say ,Python Has its own set of solutions ?

Take the two usages related to functions as examples .

When the representation function does not need to pass parameters ,f(void) This kind of writing is superfluous at all , therefore Python It uses the simplest and most straightforward nonparametric writing f().

As for the use of return value types , When we define a function , For example, the simplest def func():pass , To make its call result func() It's a legitimate object , Then it must have a valid type (type).

This should be a common problem encountered by type based programming languages ,Python No exception .

This is the time , If the function itself is not explicitly return If you have an object , There are two possible solutions :

  • Method 1 , That is, declare the function as void type , image C And what other languages do , As long as you can pass the type check
  • Method 2 , It is Python The method used , That is, the interpreter implicitly returns a None object , That is to make the function get a default value of NoneType type , Again for type checking (PS:Javascript Also similar , It's just that by default it returns undefined, It's not an object , It's a kind of expression “ Undefined ” The type of , Be similar to void)

Briefly ,Python The design idea is to directly reuse the existing NoneType type , And let the interpreter fill in the missing function types .

There are at least two benefits to this : One is that there is no introduction of new void Types and keywords ; Second, the programmer does not need to declare the return type before the function , This is consistent with writing with explicit return values .

Just imagine , If Python If the function does not have a return value by default , It might be written as void def func():... Form like this , Then it becomes a special case of function definition . Compared with another special case function , Asynchronous functions asyc def func():... , It could cause chaos .

Overall speaking ,Python It seems that void Empty types are not so necessary to exist , Seems to be NoneType Type is enough , And when the return value is missing , It's extremely convenient to have the interpreter injected uniformly , And that's what we're seeing .

thus , The question of the title of the article is a satisfactory answer .

The above is all the content shared this time , Want to know more python Welcome to official account :Python Programming learning circle , send out “J” Free access to , Daily dry goods sharing


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved