Global variable in python

I read a Python tutorial and I see the following code:

class Database:
# the database implementation
    pass

database = None

def initialize_database():
    global database
    database = Database()

Now, why initialize_databaseis there an ad inside global? We have defined databaseoutside the function, does it make it global already?

Regards,

+3
source share
2 answers

You can refer to global if it is not declared global in a function, but you can read it; writing it will create a new local variable hiding the global variable. The announcement globalallows you to record in global.

+9
source

'global x' x , " , x x ".

, x, x - .

+1

All Articles