I'm trying to understand global variables in python. But I don't know why _instance_name doesn't fail if I don't declared as global but when I try to do the same thing with _instance_counter this fail
#Module global declaration
_instance_counter = 0
_instance_name = "Employee module"
class Employe:
_internal_counter = 0
def __init__(self):
global _instance_counter#If I comment this line fails.
_instance_counter +=1 #count each instance
#why if I use _instance_name without global statement doesn't fail
print("\t**Instance of %s #%i has initialized**"%(_instance_name,_instance_counter))
def __del__(self):
print("-The class %s has deleted"%_instance_name)
def count(self):
self._internal_counter+=1
print("count method has %i time(s) called "%self._internal_counter)
def getTotalInstance(self):
print("\t %i instance of %s has been created"%(_instance_counter,__name__))
empl = Employe()
empl.count()
empl.count()
empl.count()
empl = Employe()
empl = Employe()
empl = Employe()
empl.getTotalInstance()