Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

Hey so i was wondering which is the more efficient way, or better practice to do in this situation.

1,

def function():
    global number
    number += 2

Or 2,

def function(number):
    return number += 2

Thanks.

share|improve this question

marked as duplicate by gnat, GlenH7, Bart van Ingen Schenau, amon, Snowman Feb 4 '15 at 5:04

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

6  
Did you try searching? Searching for "python local global performance" turned up this Stack Overflow question as the first result. More importantly, mutable global variables are a bad idea. – Doval Feb 3 '15 at 17:04
    
If you are working on something where the performance difference between a global and a local matters, you should probably not be using python in the first place. This is a micro-optimization at best, and python's reasons for existence don't include speed. – Steven Burnap Feb 4 '15 at 4:25

Performance is irrelevant. Globals are evil (as gnat mentioned); you are best off forgetting the global keyword exists. There is always a better way.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.