Tại sao khoảng trắng lại quan trọng trong python?
- Python không quan tâm đến các dòng trống thừa trong chương trình của bạn, giống như hầu hết các ngôn ngữ. In fact, it's common for developers to add extra empty lines, also known as white space, You can think of it kind of like paragraphs in a book. Chúng ta hãy xem tệp 03_05_begin. Things are really squished together and we even have a new function thrown in. You might find it especially challenging to figure out what this code is going to do, and that's totally understandable. Let's add a few blank lines so that it can be easier to read. We'll put a little bit of space here, some here after line number five. Much easier to read. Although in movies, programmers are often depicted as just endlessly typing on the keyboard, we actually spend much more time reading and understanding code than we do writing it. That's why we add white space. between the different elements of our statements. So right here on line number two, we're going to add some white space after name and right after this equals sign. We'll do the same thing again here on line number five. Just adding more white space, it makes it easier to read. If we save this file and run the code, Let's give it a name. And then when we hit Enter, it says, "It's nice to meet you, Jill. "Are you enjoying the course?" If we type, "Yes," then it says, "That's good to hear. " Hopefully, you were able to pick up on what the input function does. It takes some input from the user, and then it stores it in a variable, just like you see here on line number two, we get some input and then we store it in the variable name. We do this same thing here on line number five, where we ask the user if they were enjoying the course. Then we store it in our variable answer. You'll be seeing it again and again, so now you know what it does. Now let's add a bit more space. This time we're going to add space here on line number seven, after our answer variable, and once again, after this equals sign. Let's go ahead and save it and make sure we get the same output. and choose Run Python File in Terminal. Once again, we can enter a name, hit Enter, and enter yes for if we're enjoying the course. where white space really matters to Python, and that's when it relates to its special key words. If you recall, there are over 30 key words Let's take a look here on line number seven. If is one of those key words. If is one of those key words. If we delete the space between if and the answer variable, If we delete the space between if and the answer variable, let's click Save and run this code. So we'll right click once again So we'll right click once again and choose Run Python File in Terminal. and choose Run Python File in Terminal. Notice that we get a syntax error. That's because Python expects us That's because Python expects us to have a space after the if key word. to have a space after the if key word. So let's go back and add a space once again after if. So let's go back and add a space once again after if. Don't worry if you don't understand all the ins and outs Don't worry if you don't understand all the ins and outs of the current code, just keep in mind that most of the current code, just keep in mind that most of the time, Python is very laid back about white space. In fact, you're encouraged to add it in wherever it helps to improve readability. wherever it helps to improve readability. But there are circumstances where Python takes white space very seriously, and your code will fail takes white space very seriously, and your code will fail if you don't respect the rules. if you don't respect the rules. The good thing is that the more you practice, The good thing is that the more you practice, the more you follow the rules without even thinking. the more you follow the rules without even thinking Show Python is famous among programming languages for its fairly unique syntax. rather than being delimited by curly braces or “begin/end” keywords, blocks are delimited by indentation. Indenting a line is like adding an opening curly brace, and de-denting is like a closing curly brace. When people criticise Python, it is usually the first complaint. “why would I want to use a language which requires me to indent code?” Indeed, while programmers are very used to indenting their code, they are very un-used to being forced to do so, and I can understand why they may take it as an insult that a language tells them how to write code. I don’t usually like to get into syntax arguments, because I find them very superficial — it is much more important to discuss the semantics of a language than its syntax. But this is such a common argument among Python detractors, I wanted to address it. Python is right, and it’s just about the only language that is I think the rub is that programmers like to think of languages as a tool, and tools should be as flexible as possible. I think in general it is a good principle for programming languages not to enforce conventions. Languages that do tend to annoy people who don’t subscribe to the same conventions. For example, the Go programming language enforces the “One True Brace Style” — every opening curly brace must appear on the same line as the function header or control statement. This irritates me because that’s not my preferred convention. But the indentation convention is so universal that it is considered bad programming practice to not indent in all cases. (Có sự bất đồng về tab so với khoảng trắng, số lượng khoảng trắng, v.v., nhưng tất cả chúng ta đều đồng ý rằng thụt lề là tốt. ) There is not a single situation in any country, in any programming language, or at any skill level, in which is it acceptable to not indent your code the way Python requires it. Therefore, it is technically redundant to have a language that is not whitespace-sensitive. Any language that is not whitespace-sensitive requires (by universal convention) that programmers communicate the scoping of the code in two distinct manners for every single line of code. braces (or begin/end) and indentation. You are required to make sure that these two things match up, and if you don’t, then you have a program that doesn’t work the way it looks like it works, and the compiler isn’t going to tell you There are two solutions to this problem. 1. Make the compiler tell you. Force the programmer to indent and put in curly braces, and have the compiler check the indentation and give either a warning or error if they don’t match up. Now you’ve solved the problem of accidentally getting it wrong, but now what is the point of requiring curly braces at all? The programmer would just be doing extra work to please the compiler. Chúng tôi cũng có thể đi với 2. take out the curly braces and just have the compiler determine the blocks based on indentation When you really analyse it, Python’s whitespace sensitivity is actually the only logical choice for a programming language, because you only communicate your intent one way, and that intent is read the same way by humans and computers. Lý do duy nhất để sử dụng ngôn ngữ không phân biệt khoảng trắng là đó là cách chúng tôi luôn làm mọi việc và đó không bao giờ là lý do chính đáng. That is why my programming language, Mars, has the same indentation rule as Python * * * An interesting aside. có một quy tắc cú pháp liên quan trong Python có vẻ không hợp lý lắm. bạn được yêu cầu đặt dấu hai chấm ở cuối bất kỳ dòng nào trước phần thụt lề. Tôi chưa kiểm tra đầy đủ điều này, nhưng tôi khá chắc chắn rằng không có lý do kỹ thuật nào cho việc đó (trình phân tích cú pháp vẫn có thể hoạt động rõ ràng nếu không có dấu hai chấm đó) và dường như nó cũng không bổ sung nhiều vào khả năng đọc. Tôi cũng tuân theo quy tắc này một cách mù quáng ở Mars, bởi vì với tư cách là một lập trình viên Python, điều đó “cảm thấy đúng” đối với tôi. Nhưng có lẽ bỏ nó đi thì tốt hơn Chia sẻ cái nàyNhư thế nàyThích Đang tải. Có liên quan77 Bình luận 77 phản hồi cho “Tại sao quy tắc khoảng trắng của Python lại đúng”
|