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

Why should Python have a pass statement?

編輯:Python

About Python Medium pass sentence , It seems very simple ( Only 4 Letters ), Even beginners without any programming experience can quickly master its usage .

Official documents It's very simple

Briefly ,pass It's a null operation (null operation), When the interpreter executes it , In addition to checking whether the grammar is legal , Just skip without doing anything .

It goes with return、break、continue and yield And so on , The biggest difference is that it doesn't change the execution order of the program . It's like a note we wrote , Except for one line of code , It doesn't have any impact on the scope .

however , If you have the foundation of other languages , You may be curious : Why? Python There is such a unique pass sentence , Other languages don't ?

Python So designed , What's the reason ?

It is to solve the common problems that most programming languages have to face , Or because it has a new discovery of its own , So create a new feature ?

let me put it another way :Python Why would there be pass sentence , What problems can it solve ( benefits ), Without it , What problems will it cause ( Disadvantage )?

Next , This paper will analyze from two dimensions .

1、 in personam : As a space placeholder
I see it as a concise way of annotating , It means “ Let's reserve a space here , Back to the specific code implementation ”.

For example, in multi-layer if-elif-else In structure , We can write down the judgment conditions first , Then write... In the corresponding block pass, I'll improve it later .

For example, the example given above , We can write the class first / Function name and its input parameters , Then skip (pass) The main body of code , Fill in later .

pass It's easy to write , And because it's a keyword ,IDE Will give you a clear color distinction , So it's more convenient than we write notes .

pass As a space placeholder , It is mainly convenient for us to conceive the local code structure , It has a certain auxiliary reminding function .

however , As a way of annotation , It's too thin , It's not as good as writing “# todo: xxxx”, The latter will also be IDE Highlight with color , And the meaning is clearer . Although it's easy to write , But it also introduces a seemingly superfluous keyword pass.

therefore , From the perspective of spatial placeholders ,pass Not a necessary design element in a programming language .

With it , We can express “ There's something here , But skip for a while ” The semantics of the , But without it , You can use comments instead of .

2、 To the machine : For grammatical integrity
For the usage of the former ,pass The place in the code is theoretically unlimited .

however , We use... Most often pass when , Basically on the next line of the colon , And in the indented code block of this layer , There's only one sentence .( See above 3 An example , For convenience , Let's just take an empty function as an example )

We can imagine , If you don't write it , What will happen ?

The answer is an indentation error :IndentationError: expected an indented block

# Let the body of a function be pass Remove , Will report a mistake
def func():
func()

because Python Use indentation to divide code blocks , A colon indicates the appearance of a new indented block of code , So this example will report a lack of indented code block .

If we replace it with the comments mentioned above , See what happens ?

# Let the body of a function be pass Change to notes
def func():
# todo: There's something here , Fill in later
func()

Write it like this , You will also report mistakes. :IndentationError: expected an indented block

The reason is that annotations are not valid grammatical content , It will be Python The interpreter ignores (ignore), Unlike pass Sentence that is “ Effective grammatical content , But skip ”.

in other words , The indented code block must contain syntactic content , The following examples are all valid :

def func():
""" This is a string """
def func2():
123456

Python When you define a function , Must contain the body of the function , That is, it contains both declaration and definition semantics , You can't just use declarative semantics like some languages , It's written as void test(); .

however , because Python Don't use curly braces , It can't define empty functions directly like some languages , It's written as void test(){} .

Combined with the above analysis ,Python When you define an empty function , There has to be a legal body of functions , Therefore, we design a method to represent null operation pass sentence . It's to complement the integrity of grammar , With colon , Equivalent to a pair of empty curly braces in other languages .

From the perspective of grammatical integrity , It's a necessary design element , If not , It must also be replaced by similar empty statements or special symbols .

On the human side ,pass Can be said “ Skip for now ” The meaning of , As a temporary place holder , It will eventually be replaced by the actual code implementation ; On the machine side , It can express “ Just skip ”, Just to complement grammar and logic , It won't be replaced by other code .

Other languages don't have a special statement or symbol to represent this kind of place holder ( There is a lack of semantics ), But they don't need to design a keyword to complement the grammatical integrity ( That is, the grammar is complete ).

Back to the question at the beginning of this article :Python Why would there be pass sentence , What problems can it solve ( benefits ), Without it , What problems will it cause ( Disadvantage )?

Python Use pass sentence , It's a code block to support pure null operations ( Empty function 、 Empty class 、 Empty loop control blocks and so on ), With it , It can also express the semantics of a place holder .

The former is for machines , There must be , It is equivalent to the function of curly braces in other languages ; The latter is for people , Not a must , It can be expressed in comments , But because Python Designed this statement , This usage is quite convenient sometimes .

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