预计python中有两个空行pep8警告
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33466860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
expected two blank lines pep8 warning in python
提问by Amit Upadhyay
I'm using vim editor as python IDE. Below is a simple python program to calculate square root of a number:
我使用 vim 编辑器作为 python IDE。下面是一个简单的python程序来计算一个数字的平方根:
import cmath
def sqrt():
try:
num = int(input("Enter the number : "))
if num >= 0:
main(num)
else:
complex(num)
except:
print("OOPS..!!Something went wrong, try again")
sqrt()
return
def main(num):
squareRoot = num**(1/2)
print("The square Root of ", num, " is ", squareRoot)
return
def complex(num):
ans = cmath.sqrt(num)
print("The Square root if ", num, " is ", ans)
return
sqrt()
And the warnings are :
警告是:
1-square-root.py|2 col 1 C| E302 expected 2 blank lines, found 0 [pep8]
1-square-root.py|15 col 1 C| E302 expected 2 blank lines, found 1 [pep8]
1-square-root.py|21 col 1 C| E302 expected 2 blank lines, found 0 [pep8]
Can you please tell why these warnings are coming?
你能说出为什么会出现这些警告吗?
采纳答案by Leb
import cmath
def sqrt():
try:
num = int(input("Enter the number : "))
if num >= 0:
main(num)
else:
complex_num(num)
except:
print("OOPS..!!Something went wrong, try again")
sqrt()
return
def main(num):
square_root = num**(1/2)
print("The square Root of ", num, " is ", square_root)
return
def complex_num(num):
ans = cmath.sqrt(num)
print("The Square root if ", num, " is ", ans)
return
sqrt()
The previous will fix your PEP8problems. After your import you need to have 2 new lines before starting your code. Also, between each def foo()
you need to have 2 as well.
前一个将解决您的PEP8问题。导入后,您需要在开始代码之前添加 2 行新行。此外,在每个之间,def foo()
您还需要有 2 个。
In your case you had 0 after import, and you had 1 newline between each function. Part of PEP8 you need to have a newline after the end of your code. Unfortunately I don't know how to show it when I paste your code in here.
在您的情况下,导入后有 0,并且每个函数之间有 1 个换行符。PEP8 的一部分,您需要在代码结束后换行。不幸的是,当我在这里粘贴您的代码时,我不知道如何显示它。
Pay attention to the naming, it's part of PEP8 as well. I changed complex
to complex_num
to prevent confusion with builtin complex
.
注意命名,它也是 PEP8 的一部分。我更改complex
为complex_num
以防止与 builtin 混淆complex
。
In the end, they're only warning, they can be ignored if needed.
最后,它们只是警告,如果需要可以忽略它们。
回答by Balaji Wanole
You need to give two blank lines between meaningful code blocks.
您需要在有意义的代码块之间给出两个空行。
These include (for example):
这些包括(例如):
- The import block
- Each function
- 导入块
- 各功能
回答by Lakshmikant Deshpande
Here is the link to the documentation:
PEP8 Style Guide for Python
You should add two spaces between the functions, as shown below:
这是文档的链接:
PEP8 Style Guide for Python
你应该在函数之间添加两个空格,如下所示:
import cmath
def sqrt():
try:
num = int(input("Enter the number : "))
if num >= 0:
main(num)
else:
complex_num(num)
except:
print("OOPS..!!Something went wrong, try again")
sqrt()
return
def main(num):
square_root = num ** (1 / 2)
print("The square Root of ", num, " is ", square_root)
return
def complex_num(num):
ans = cmath.sqrt(num)
print("The Square root if ", num, " is ", ans)
return
sqrt()
回答by Balaji Wanole
with warnings:-
import math
def my():
print("hello world")
my()
Without warnings:-
import math
def my():
print("hello world")
my()
Here if you see the two lines space after import statement for second code snippet which will not give any warnings. Again if you are writing two methods definition you have two give two lines as space between your code block.
在这里,如果您在第二个代码片段的 import 语句之后看到两行空格,则不会给出任何警告。同样,如果您正在编写两个方法定义,则您的代码块之间有两行空格。
回答by serv-inc
All answers seem to be correct. To avoid doing this by hand, you can also use the autopep8
package(pip install autopep8). The result of calling autopep8 filename.py
is the same:
所有的答案似乎都是正确的。为避免手动执行此操作,您还可以使用autopep8
包(pip install autopep8)。调用的结果autopep8 filename.py
是一样的:
import cmath
def sqrt():
try:
num = int(input("Enter the number : "))
if num >= 0:
main(num)
else:
complex(num)
except:
print("OOPS..!!Something went wrong, try again")
sqrt()
return
def main(num):
squareRoot = num**(1/2)
print("The square Root of ", num, " is ", squareRoot)
return
def complex(num):
ans = cmath.sqrt(num)
print("The Square root if ", num, " is ", ans)
return
sqrt()