0

I was just wondering, in python 3, why do some global variables have to be defined as global every singe function you use (that you're defining), but others only need to be be defined once? Probably a question because I haven't understood scope properly, please could someone shed some light?

awsomeguy
  • 191
  • 3
  • 11

1 Answers1

3

Variables in a scope can be read but not modified by functions/objects declared within that scope. If you also wish to change their value, you have to declare them as global.

UPDATE: However, please note, that "changing the value" may or may not mean what you are eventually used to if comming from other programming languages, as assignment in Python means giving an object a name. The keyword global allows you to let the given name to point to (being associated with) a different object. Look at the following example:

aa = 1
bb = 2
cc = [ 1, 2, 3 ]

def test():
    global aa
    aa = -1
    bb = -1
    cc[2] = -1

test()
print "aa:", aa
print "bb:", bb
print "cc:", cc

would result in:

aa: -1
bb: 2
cc: [1, 2, -1]
  • In the function test() the name aa is declared as global, so aa = -1 in the function would cause to point the global name aa to a new object, namely the integer -1.

  • The name bb in test() is not declared as global, therefore the assignment bb = -1 would create the name bb locally within the function and point it to -1, while leaving the name bb in the global scope still pointing to the value 2.

  • Variable cc is also not declared as global, therefore, you can't change the association of the global cc in the function test(). However, since in the global scope cc is associated with a mutable object, you can change the content of this object within the function.

Note, that the formulation here is maybe not the official Python way of explaining the differences, but it may help to understand things if you have some background in other programming languages.

Bálint Aradi
  • 3,754
  • 16
  • 22
  • Note that the correct interpretation of this depends entirely on the correct interpretation of "change the value of a variable". History shows that beginners frequently get that part wrong. –  Feb 25 '13 at 19:34
  • I agree, I find therefore the thread you linked in above a very good starting point about clarifying the fine details (with respect of mutable an immutable types). – Bálint Aradi Feb 25 '13 at 19:37
  • That you mention mutable and immutable types as if there was a distinction makes it sound like you don't understand it either. –  Feb 25 '13 at 19:40
  • Sorry, if my formulation was misunderstandable. I've made the experience, that people comming from other programming languages often have difficulties to understand, why mutable and inmutable types behave differently, if they try to explain things with their experiences made in other programming languges. I agree, that if you once know how Python works, their behaviour is consistent. I now tried to explain this somewhat more in detail above. – Bálint Aradi Feb 25 '13 at 20:15
  • What I'm talking about is that they don't behave differently at all. –  Feb 25 '13 at 20:25