Im creating a simple python program that gives basic functionality of an SMS_Inbox. I have created an SMS_Inbox method.
store = []
message_count = 0
class sm
-
You are trying to assign to a global variable message_count
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 the variable as a class attribute:
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 have no state anymore, so there is no point in creating a class here. It only serves to confuse your purpose.
Either store state in instances, or use global functions (so don't use a class at all); the former is preferable to the latter.
Transforming your setup to a class whose instances hold state, using proper PEP-8 styleguide naming and string 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
You are then free to create one instance and use that as a global if needs be:
sms_store = SMSStore()
Other code just uses sms_store.add_new_arrival(...)
, but the state is encapsulated in one instance.
- 热议问题