Python样式指南:间歇变量 [英] Python style guide: intermittent variables
问题描述
我一直在寻找有关间歇变量"和可读性的Python样式指南.我开发的代码将主要由非编程专家使用,因此应该易于阅读,但另一方面,我想学习标准的Python样式.
I was searching for a Python style guide in terms of "intermittent variables" and readability. 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.
一个简单的例子:
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))))
我想在这种情况下,第2版并不是很难读懂,但我希望您知道我的意思.
I guess in this case, Version 2 is not really hard to read, but I hope you know what I mean.
- 此间歇变量"的正确词是什么(如果有)?是否有样式指南等可用的东西?
- 在计算时间方面(版本1与版本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. 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.
在创建一些临时变量时,应该没有明显的性能差异.执行代码的两个不同版本所需的内存分配和处理实际上是相同的.
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.
这篇关于Python样式指南:间歇变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!