语法中的空格会影响 Python 中的性能吗?
Does whitespace in syntax affect performance in Python?
当我为个人项目编写代码时,或者如果我只是在测试一些东西,我倾向于这样编码,只是因为它让我开心:
def importcontacts(request):
context = initialize_context(request)
context['form'] = UploadedFileForm()
token = get_token(request)
if request.method == 'POST':
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
contacts = request.FILES['file']
fs = FileSystemStorage()
filename = fs.save('import_data.json', contacts)
uploaded_file_url = fs.url(filename)
context['fails'] = ct.import_contacts(uploaded_file_url,
token,
context['user']['email'],
context['user']['name'])
messages.success(request, 'Contacts imported successfully.')
return render(request, 'contactsdb/importresult.html', context)
return render(request, 'contactsdb/import.html', context)
显然这在任何方面都不符合 PEP8,我永远不会将这样的东西投入生产,但与此同时我真的不知道为什么,我也不真正理解为什么代码仍然存在像这样开始工作。我假设所有 space 都会使代码变慢?
谷歌搜索没有帮助我找到答案。我不是在找人告诉我“你永远不应该那样做,等等”,我很清楚!我只想知道为什么这不行。
这是一个有趣的问题。基本上在操作数和运算符之间使用白色 space 是为了增加可读性。加一白space还是加十只是个人选择。 interpreter/compiler 不关心白色 space。这只是关于可读性。
现在当你做这样的事情时-
a = 10
b = 100
c = a + b
当你做这样的事情时-
a = 10
b = 100
c = a + b
您会注意到第一个比第二个更易读,但这并不意味着第二个是错误的。这只是个人选择的问题。基本上惯例说要遵循第一种方法,但是无论有没有白色 spaces.
我们都会得到相同的输出
所以你可以在你的程序中使用一个或十个白色的space,没有人可以质疑你!!
间距不应减慢代码速度。与任何其他语言非常相似,您的 python 脚本会被编译为字节码,然后由虚拟机进行解释。解析器通常会去除不是缩进或换行符的注释和空格。 link 进一步解释了如何处理逻辑行:
The end of a logical line is represented by the token NEWLINE. Statements cannot cross logical line boundaries except where NEWLINE is allowed by the syntax (e.g., between statements in compound statements). A logical line is constructed from one or more physical lines by following the explicit or implicit line joining rules.
和this one解释了如何对待物理的:
A physical line ends in whatever the current platform's convention is for terminating lines. On Unix, this is the ASCII LF (linefeed) character. On DOS/Windows, it is the ASCII sequence CR LF (return followed by linefeed). On Macintosh, it is the ASCII CR (return) character.
这 link 进一步解释了如何处理缩进:
Leading whitespace (spaces and tabs) at the beginning of a logical line is used to compute the indentation level of the line, which in turn is used to determine the grouping of statements.
并且还给出了一个正确但缩进奇怪的代码示例:
def perm(l):
# Compute the list of all permutations of l
if len(l) <= 1:
return [l]
r = []
for i in range(len(l)):
s = l[:i] + l[i+1:]
p = perm(s)
for x in p:
r.append(l[i:i+1] + x)
return r
当我为个人项目编写代码时,或者如果我只是在测试一些东西,我倾向于这样编码,只是因为它让我开心:
def importcontacts(request):
context = initialize_context(request)
context['form'] = UploadedFileForm()
token = get_token(request)
if request.method == 'POST':
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
contacts = request.FILES['file']
fs = FileSystemStorage()
filename = fs.save('import_data.json', contacts)
uploaded_file_url = fs.url(filename)
context['fails'] = ct.import_contacts(uploaded_file_url,
token,
context['user']['email'],
context['user']['name'])
messages.success(request, 'Contacts imported successfully.')
return render(request, 'contactsdb/importresult.html', context)
return render(request, 'contactsdb/import.html', context)
显然这在任何方面都不符合 PEP8,我永远不会将这样的东西投入生产,但与此同时我真的不知道为什么,我也不真正理解为什么代码仍然存在像这样开始工作。我假设所有 space 都会使代码变慢?
谷歌搜索没有帮助我找到答案。我不是在找人告诉我“你永远不应该那样做,等等”,我很清楚!我只想知道为什么这不行。
这是一个有趣的问题。基本上在操作数和运算符之间使用白色 space 是为了增加可读性。加一白space还是加十只是个人选择。 interpreter/compiler 不关心白色 space。这只是关于可读性。
现在当你做这样的事情时-
a = 10
b = 100
c = a + b
当你做这样的事情时-
a = 10
b = 100
c = a + b
您会注意到第一个比第二个更易读,但这并不意味着第二个是错误的。这只是个人选择的问题。基本上惯例说要遵循第一种方法,但是无论有没有白色 spaces.
我们都会得到相同的输出所以你可以在你的程序中使用一个或十个白色的space,没有人可以质疑你!!
间距不应减慢代码速度。与任何其他语言非常相似,您的 python 脚本会被编译为字节码,然后由虚拟机进行解释。解析器通常会去除不是缩进或换行符的注释和空格。 link 进一步解释了如何处理逻辑行:
The end of a logical line is represented by the token NEWLINE. Statements cannot cross logical line boundaries except where NEWLINE is allowed by the syntax (e.g., between statements in compound statements). A logical line is constructed from one or more physical lines by following the explicit or implicit line joining rules.
和this one解释了如何对待物理的:
A physical line ends in whatever the current platform's convention is for terminating lines. On Unix, this is the ASCII LF (linefeed) character. On DOS/Windows, it is the ASCII sequence CR LF (return followed by linefeed). On Macintosh, it is the ASCII CR (return) character.
这 link 进一步解释了如何处理缩进:
Leading whitespace (spaces and tabs) at the beginning of a logical line is used to compute the indentation level of the line, which in turn is used to determine the grouping of statements.
并且还给出了一个正确但缩进奇怪的代码示例:
def perm(l):
# Compute the list of all permutations of l
if len(l) <= 1:
return [l]
r = []
for i in range(len(l)):
s = l[:i] + l[i+1:]
p = perm(s)
for x in p:
r.append(l[i:i+1] + x)
return r