Can't use global variables within a function

hi again guys.
Today i’m trying out functions, but i’ve run in a small (inconsistancy?) issue.
Mainly i’m trying to print out global variables within a function, which works,
but when I try to change the value of the pre-created variables, from inside the function, its not allowing me. I’ve pasted the error in the comment near the commands

a = 1
b = 2
string1 = ""

def test_print():
  print (a)                      #- works fine 
  print (b)                      #- works fine 
  print(string1)                 #- works fine 
  #string1 += "hello"            #- not working         // error : referenced before assignment
  #string1 = string1 + "hello"   #- not working either // error : referenced before assignment


If place string1 = “” inside the function the error goes, but thats not what I want, in case I’ll have a value in string1 which I do not want to overwrite.

thanks in adv

edit forgot to say that i did read about the error in the documentation which says that since theres no need to declare variables before hand, the assignment within a function makes the variable local? still didnt quite understand it completely why, and even if so, is it possible to use the same variable without losing whatever value is stored inside it?

Hello, @dave_lee,

This may help: Python Documentation: 7.12. The global statement.

Inside the function, you need:

  global string1

EDIT (November 19, 2021):

When a variable is assigned a value within a function, the variable is local by default. Otherwise, we would often change the values of global variables unintentionally within a function.

1 Like

@Quercus thanks for the ‘global’ tip. wasnt aware of that. Its kind of confusing though since this way it feels like local variables are given preference over global. I would have thought that at most if you re-declare it locally it would over write the global value

That is exactly what happens: local variables are given precedence
over globals. No variable name can be both local and global inside the
same function: it must be one or the other, so you can’t start by using
a global and then turn it into a local.

This is a simplified description of how Python decides whether to treat
a name inside a function as a local or global.

(1) The parameters of the function are always local variables:

def func(a, b, c):
    # a, b and c are now local variables.

(2) Names which are assigned to are local by default.

Assignment includes:

variable = 27     # variable will be local
del variable      # variable is a local
import math       # math will be local
for i in values:  # i will be local

Yes, it is a bit odd that deleting a variable counts as assignment!

(3) But if you declare the variable as global first, then Python
will treat it as global even if you assign to it.

(4) Otherwise names that you don’t declare as global, and never assign
to, are treated as either global or builtin names.

So you can read from globals without declaring them:

number = 1

def func():
    print(number)  # this is fine, will print the global

But if you assign a new value to it, anywhere in the function, it
becomes local:

def func():
    print(number)  # this will fail: local variable has no value
    number = 2

Unless you declare it global first:

def func():
    global number
    print(number)  # this is fine
    number = 2

That seems a good feature, otherwise it would be too easy for a function to inadvertently wreak havoc in the global namespace.