1

If I name the variables long in Python would it affect the memory consumed by the program? Is variable a better in terms of memory than variable this_is_a_long_variable_name

I_Need_Coffee
  • 59
  • 1
  • 1
  • 10
  • 1
    When CDs came out, Bill gates was [showing off](https://pbs.twimg.com/media/DL2KneCWkAEbIbA.jpg) exactly how much text a CD can hold. Average RAM (internal memory) of a modern computer these days is 10-20 times that. `this_is_a_long_variable` (using proper Python sytle, variables should be written in snake-case, with underscores separating words) does use more memory than `a`, just like eating a bacterium is more food than not eating a bacterium. – Amadan Jun 18 '19 at 03:43
  • I don’t think so, but you would want to have your. Variable names ling enough to be descriptive and pep-8 compliant, also check out https://stackoverflow.com/questions/48721582/how-to-choose-proper-variable-names-for-long-names-in-python – Devesh Kumar Singh Jun 18 '19 at 03:44

1 Answers1

4

Names are stored once in the compiled byte code for debugging purposes, but all access to them is done via integer indices referencing their position in the appropriate namespace. Consider the following example:

>>> import dis
>>> dis.dis("a=0;thisisalongvariablename=1")
  1           0 LOAD_CONST               0 (0)
              2 STORE_NAME               0 (a)
              4 LOAD_CONST               1 (1)
              6 STORE_NAME               1 (thisisalongvariablename)
              8 LOAD_CONST               2 (None)
             10 RETURN_VALUE

As far as the interpreter is concerned, there are simply two global variables "named" 0 and 1; a and thisisalongvariablename are just labels in the source code.

Don't worry about name length beyond the readability of your code.

chepner
  • 497,756
  • 71
  • 530
  • 681