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


  • 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. – Gort the Robot 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.

| improve this answer | |

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