8

It is a basic question. I am trying the following code:

class SMS_store:

def __init__(self):
    self=[]    #probably something is wrong here

def add_new_arrival(self,from_number,time_arrived,text_of_SMS):
    self.append([False,from_number,time_arrived,text_of_SMS])    #append list to self list
    self[len(self)-1]=tuple(self[len(self)-1])

def message_count(self):
    return len(self)

my_inbox=SMS_store()
my_inbox.add_new_arrival('01234','9:37 AM','How are you?')

But I get the following error:

>>> 
Traceback (most recent call last):
  File "C:\Users\Arnob\Desktop\New Text Document.py", line 15, in <module>
    my_inbox.add_new_arrival('01234','9:37 AM','How are you?')
  File "C:\Users\Arnob\Desktop\New Text Document.py", line 8, in add_new_arrival
    self.append([False,from_number,time_arrived,text_of_SMS])    #append list to self list
AttributeError: 'SMS_store' object has no attribute 'append'
>>>

What is wrong in my code?

3 Answers 3

6

You can subclass list like this

class SMS_store(list):

    def add_new_arrival(self, from_number, time_arrived, text_of_SMS):
        self.append((False, from_number, time_arrived, text_of_SMS))    #append tuple to self

    def message_count(self):
        return len(self)

Notice there is no need for __init__ unless you wish to do something extra there.

You don't need to append a list and then turn it into a tuple, you can create the tuple directly with () instead of []

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I've figured it out. Your last answer has also helped me.
5

If you want to inherit from list, use the following:

class SMS_store(list):
               ^^^^^^

and remove that assignment to self from the __init__ method.

That said, you might want to simply have a named attribute containing the list:

class SMS_store(object):

   def __init__(self):
      self.messages = []

   def add_new_arrival(self, from_number, time_arrived, text_of_SMS):
      self.messages.append((False,from_number,time_arrived,text_of_SMS))

   def message_count(self):
      return len(self.messages)

my_inbox = SMS_store()
my_inbox.add_new_arrival('01234','9:37 AM','How are you?')

As far as representing actual messages, this sounds like a good use case for namedtuple. It's just like a tuple, but allows access to fields by name. Here is a quick illustration:

import collections

SMS = collections.namedtuple('SMS', 'from_number time_arrived text_of_SMS')

sms = SMS(from_number='01234', time_arrived='9:37 AM', text_of_SMS='How are you?')
print sms.text_of_SMS

1 Comment

Thank you! I've figured it out.
2

You need to still create a normal variable name, just put self. as a prefix:

self.mylist = []

To access it, you do something like:

self.mylist.append(n)

Or:

self.mylist[3] = 'hi'

You're actually overriding self. You don't want to do that.

1 Comment

Thank you! I've figured it out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.