0

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)?
Kenni
  • 437
  • 1
  • 4
  • 12
  • 1
    Version 2 is probably okay, if you rename `c1` with a more descriptive and explicit name – Reblochon Masque Sep 28 '17 at 05:35
  • 1
    “intermediate variables”, and no, there is no significant performance difference. – Ry- Sep 28 '17 at 05:43
  • 1
    Generally variables are properly named for our convenience only, if someone is revisiting the code after some time gap then code is easy to understand if vars are named properly. If you think naming certain variable as `a1` will not affect readability then it is fine in my opinion. – anuragal Sep 28 '17 at 05:44

1 Answers1

0

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.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880