Curious
Curious

Reputation: 815

Python Efficiency: is it better to create new variables and assign tasks to it rather than keep on using same variable?

I apologise if this question has been asked many times before but I am probably using wrong terms to find the answer.

I am using Rasberry Pi so I care about efficiency. in Python script I have a variable called foo and I need to strip it and then make a list of it. So far this is how my code looks like:

 foo = "hello, world"
 foo = foo[1:-1]         #strip away the quotation marks.
 foo = foo.split(", ")   #make a list of it

My question is: is it okay if I keep on using same variable or should I create new ones? Or should I do something like this:

foo = "hello, world"
bar = foo[1:-1]
fubar = bar.split(", ")

Furthermore, foo variable keeps getting different strings many times a second. I mean with every instance of that variable, there is a new memory allocation, but does it mean that I should keep on using same variable then?

Thank you for your answers and again, apologise for stupid question.

Upvotes: 1

Views: 1983

Answers (2)

ShadowRanger
ShadowRanger

Reputation: 155353

It's largely a stylistic choice. Using a single name will be slightly more efficient and avoid keeping temporaries around after they stop being needed, but it's usually a trivial difference. Function locals are (in CPython anyway) just stored as pointers in an array for the function's scope; storing three pointers instead of one is not a meaningful distinction unless it ends up keeping alive a huge object.

Stick to new names when the meaning of the data stored changes. In particular, it's bad form to reuse a variable for data of a completely different type or purpose across a long function (it confuses maintainers when it has one meaning in lines 1-20, and a different one in 40-50).

In your specific case, the first two values stored seem correlated; it's probably okay to reuse the name. I'd be leery of reusing the name after the split though; switching the type of a variable is often bad form (the exceptions being when you're doing initial parsing of something that is a str that logically represents a sequence of values, which could be the case here).

Basically, don't be needlessly confusing.

Side-note: When I see foo.split(", "), that's code smell. If you're working with CSV data, use the csv module, don't roll your own CSV parser with terrible edge cases and errors.

Upvotes: 2

M. Powell
M. Powell

Reputation: 69

In terms of time efficiency, the differences between the two approaches are negligible. The interpreter will need to dynamically allocate memory regardless of whether or not they are referenced by the same variable.

As far as space efficiency, the python interpreter will deallocate the memory as soon as you leave the scope in which it has references. For example, if this operation is being executed inside of a function definition or a loop, the memory will be deallocated at the end of every activation of this function or loop regardless of whether the memory is in one location or three.

For these reasons, I would suggest sticking with three separate variables to make it clear when you need to look at this later on that you are dealing with three distinct conceptual pieces of information. If later on you discover that this has somehow become a bottleneck, you can always go back and optimize.

Upvotes: 2

Related Questions