如何解决 Python 中的 SyntaxError [英] How to tackle SyntaxError in Python

查看:53
本文介绍了如何解决 Python 中的 SyntaxError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于 Stack Overflow 上有一堆关于 Python 中 SyntaxError 的问题,我们可能想知道:

Since there are a bunch of questions here on Stack Overflow that deal with SyntaxError in Python, we might want to know:

我们如何解决SyntaxError?是否有普遍适用的策略?

How do we tackle a SyntaxError? Are there strategies that can generally be applied?

推荐答案

0.错误出现之前:语法高亮和代码格式

即使在遇到 SyntaxError 之前,也有一些重要的措施来处理 SyntaxErrors,因为处理 SyntaxErrors 的最佳方法是首先避免它们.这首先可以通过使用具有语法高亮<的编辑器或集成开发环境 (IDE) 来完成/a> 用于 Python.

0. Before the Error appears: Syntax Highlighting and Code Formatting

Even before running into a SyntaxError, there are important measurements to deal with SyntaxErrors, because the best way to deal with SyntaxErrors is to avoid them in the first place. This can be done first and foremost by using an editor or an Integrated Development Environment (IDE) which has syntax highlighting for Python.

除此之外,我们可以通过良好的代码和格式样式来降低遇到 SyntaxError 的风险.术语良好的格式样式"有一个正式的定义, PEP 8 -- StylePython 代码指南.正确的格式使我们的代码更具可读性,从而降低编写导致 SyntaxError 的代码的风险.

Besides that, we can decrease the risk of running into a SyntaxError by good code and formatting style. There is a formal definition of the term "good formatting style", PEP 8 -- Style Guide for Python Code. Proper formatting makes our code much more readable which decreases the risk writing code that leads to a SyntaxError.

对我们的代码应用良好格式的一个很好的方法是使用自动代码格式工具.代码格式化程序具有多种优点,其中包括: 代码格式化一致.它应用了您可能从未想过的最佳实践.很方便.

A very good way to apply good formatting to our code is to use an automatic code formatting tool. A code formatter has multiple advantages, amongst which are the following: Its code formatting is consistent. It applies best practices you might not even have thought of yet. It is very convenient.

对于 Python,black 是一个很棒的代码格式化工具.

For Python, black is a great code formatting tool.

  1. 语法错误表明解释器在我们的代码中遇到了问题的哪个文件和哪一行.我们应该使用这些信息来查找错误.

  1. The Syntax Error indicates in which file and in which line the interpreter came across a problem in our code. We should use this information to find the bug.

我们应该知道,Python 解释器有时会在实际问题的之后行中指示SyntaxError.这是因为解析器期望错误行中有某些内容,并且只有在整行都已被解析后才能识别出缺少的内容.这种 SyntaxError 的原型示例是缺少括号.因此,例如,以下代码在行 2 中引发 SyntaxError,即使错误在行 1 中:

We should be aware that the Python interpreter sometimes indicates a SyntaxError in the line after the actual problem. This is because the parser expects something in the erroneous line and can recognise that this is missing only when the whole line has been parsed. The prototypic example for that kind of SyntaxError is a missing parenthesis. So for instance, the following code raises a SyntaxError in line 2, even though the bug is in line 1:

bar = foo(
baz()

  1. EOL 代表行尾".这有助于理解非常常见的SyntaxError: EOL while scaning string literal.这通常是在您没有使用右引号正确关闭字符串定义时引发的,例如在以下示例中:
  1. EOL stands for "End Of Line". This helps understanding the very common SyntaxError: EOL while scanning string literal. This is usually raised when you did not properly close a string definition with closing quotation marks, such as in the following example:

foo = "bar

2.简化代码

通常,修复错误的一个好策略是将任何引发错误或异常(或不返回预期输出)的代码减少到 最小示例.(这是在 Stack Overflow 上提问的必要条件,但更重要的是,这是一种确定错误的好方法.)

2. Simplify the Code

Generally, a good strategy of bug fixing is to reduce any code that throws an Error or an Exception (or that does not return the expected output) to a minimal example. (This is a requirement for questions here on Stack Overflow, but much more than this, it is a good technique for pinning down a bug.)

SyntaxError 的情况下,生成一个最小的例子通常很容易,因为 SyntaxError 不依赖于变量的任何值,或任何状态对象或代码的任何其他语义.这就是为什么 SyntaxError 的来源通常是一行代码.

In case of a SyntaxError, producing a minimal example is usually very easy, because a SyntaxError does not depend on any values of a variable, or any state of an object or any other semantics of your code. That's why the source of a SyntaxError is usually one line of code.

因此,为了识别错误,我们删除了除我们认为是错误来源的行之外的所有代码.如果错误消失,则它已在不同的行中.如果错误仍然存​​在,我们尝试简化这一行.例如,我们通过定义保存值的中间变量来替换嵌套的括号:

So, to identify the bug, we remove all the code besides the line that we think is the source of the Error. If the Error vanishes, it has been in a different line. If the Error persists, we try to simplify this line. For instance, we replace nested parentheses, by defining intermediate variables that hold the values:

代替

bar = foo(foo(baz(foo()))

以下(逻辑上等价的)代码:

the following (logically equivalent) code:

first = foo()
second = baz(first)
third = foo(second)
bar = foo(third

让我们更容易识别缺失的右括号.

makes it much easier for us to identify the missing closing parenthesis.

这篇关于如何解决 Python 中的 SyntaxError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆