Global variable and Python class

Im creating a simple python program that provides the basic functions of SMS_Inbox. I created the SMS_Inbox method.

store = [] message_count = 0 class sms_store: def add_new_arrival(self,number,time,text): store.append(("From: "+number, "Recieved: "+time,"Msg: "+text)) **message_count += 1** def delete(self,i): if i > len(store-1): print("Index does not exist") else: del store[i] message_count -= 1 

In bold bit I get an error message:

 UnboundLocalError: local variable 'message_count' referenced before assignment. 

I created a global variable store that is an empty list, and this works when I use the add_new_variable object. However, for some reason, it is not adding values ​​to my global message_count variable.

Please, help

+7
source share
3 answers

This is not how classes work. Data should be stored inside an instance of the class, not globally.

 class SMSStore(object): def __init__(self): self.store = [] self.message_count = 0 def add_new_arrival(self,number,time,text): self.store.append(("From: "+number, "Recieved: "+time,"Msg: "+text)) self.message_count += 1 def delete(self, i): if i >= len(store): raise IndexError else: del self.store[i] self.message_count -= 1 sms_store = SMSStore() sms_store.add_new_arrival("1234", "now", "lorem ipsum") try: sms_store.delete(20) except IndexError: print("Index does not exist") print sms_store.store # multiple separate stores sms_store2 = SMSStore() sms_store2.add_new_arrival("4321", "then", "lorem ipsum") print sms_store2.store 
+10
source

If the variable you are referring to is message_count , the error is that in Python you need to specify the variable as global before you can edit it.

That should work.

 store = [] message_count = 0 class sms_store: def add_new_arrival(self,number,time,text): global message_count store.append(("From: "+number, "Recieved: "+time,"Msg: "+text)) message_count += 1 def delete(self,i): if i > len(store-1): print("Index does not exist") else: global message_count del store[i] message_count -= 1 

As written above, you'd better encapsulate it in the __init__ function instead of the global declaration.

+6
source

You are trying to assign a message_count global variable without declaring it as such:

 message_count = 0 class sms_store: def add_new_arrival(self,number,time,text): store.append(("From: "+number, "Recieved: "+time,"Msg: "+text)) global message_count message_count += 1 

Try to avoid using globals or at least encapsulate a variable as an attribute of a class:

 class sms_store: message_count = 0 store = [] def add_new_arrival(self,number,time,text): sms_store.append(("From: "+number, "Recieved: "+time,"Msg: "+text)) sms_store.message_count += 1 

However, your class instances no longer have a state, so it makes no sense to create a class. This only confuses your goal.

either save state in instances, or use global functions (so don't use the class at all); the former is preferable to the latter.

Converting your installation to a class whose instances are state-safe using the correct PEP-8 style notation and line formatting:

 class SMSStore(object): def __init__(self): self.store = [] self.message_count = 0 def add_new_arrival(self, number, time, text): self.store.append('From: {}, Received: {}, Msg: {}'.format(number, time, text)) self.message_count += 1 

Then you can create one instance and use it as a global one if necessary:

 sms_store = SMSStore() 

Another code just uses sms_store.add_new_arrival(...) , but the state is encapsulated in one instance.

+1
source

All Articles