简体   繁体   中英

Python style guide: intermittent variables

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.

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.

  • What is the correct word for this "intermittent variables" (if there is one)? Is there a something available like a style guide?
  • Is there a big difference in terms of computation time (Version 1 vs. Version 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM