I_Need_Coffee
I_Need_Coffee

Reputation: 59

Does very long variable names in python result in wastage of memory?

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

Upvotes: 1

Views: 348

Answers (1)

chepner
chepner

Reputation: 530960

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.

Upvotes: 4

Related Questions