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.


def function():
    global number
    number += 2

Or 2,

def function(number):
    return number += 2


marked as duplicate by gnat, GlenH7, Bart van Ingen Schenau, amon, user22815 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.

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