Reputation: 59
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
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