[英]Python style guide: intermittent variables
I was searching for a Python style guide in terms of "intermittent variables" and readability. 我正在寻找有关“间歇变量”和可读性的Python样式指南。 The code that I develope will be mainly used by non-programming-experts, so it should be easy to read, but on the other hand, I would like to learn standard Python style.
我开发的代码将主要由非编程专家使用,因此应该易于阅读,但另一方面,我想学习标准的Python风格。
A simple example: 一个简单的例子:
import numpy as np
a = [2,3,5,4]
b = [2,2,2,2]
#Version 1
a1 = np.array(a)
b1 = np.array(b)
a2 = np.transpose(a1)
b2 = np.transpose(b1)
c = np.vstack((a2,b2))
#Version 2
c1 = np.vstack((np.transpose(np.array(a)), np.transpose(np.array(b))))
I guess in this case, Version 2 is not really hard to read, but I hope you know what I mean. 我想在这种情况下,第2版并不是很难阅读,但我希望您知道我的意思。
The correct words are either intermediate variable or temporary variable . 正确的单词是中间变量或临时变量 。 I tend more towards the style in version 1 of your example code, so I use them a lot, but I also tend to prefer more descriptive variable names when I use them.
我倾向于您的示例代码的版本1中的样式,因此我经常使用它们,但是当我使用它们时,我也倾向于使用更具描述性的变量名。 One benefit of version 2 of your code is that this isn't really necessary, since the nested function calls already describe what's happening.
代码版本2的一个好处是,这实际上不是必需的,因为嵌套函数调用已经描述了正在发生的事情。
There should be no significant performance difference in creating a few temporary variables. 创建一些临时变量应该没有明显的性能差异。 The memory allocation and processing required to execute the two different versions of your code are virtually identical.
执行代码的两个不同版本所需的内存分配和处理实际上是相同的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.