Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create List Object Class in Python

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?

like image 424
arnobpl Avatar asked Jul 04 '13 04:07

arnobpl


People also ask

Can I make a list of classes in Python?

Python list can hold a list of class objects. We can create one empty list and append multiple class objects to this list. Each list element will be an object, and we can access any member of that object like method, variables, etc. Note that you can append different class objects to the same list.

Can I create object of list?

You could create a list of Object like List<Object> list = new ArrayList<Object>() . As all classes implementation extends implicit or explicit from java. lang. Object class, this list can hold any object, including instances of Employee , Integer , String etc.

Can Python list contain object?

List object is the more general sequence provided by Python. Lists are ordered collections of arbitrarily typed objects. They have no fixed size. In other words, they can hold arbitrary objects and can expand dynamically as new items are added.

Can I create an object class in Python?

Creating an Object in Python We saw that the class object could be used to access different attributes. It can also be used to create new object instances (instantiation) of that class. The procedure to create an object is similar to a function call. This will create a new object instance named harry .


3 Answers

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 []

like image 50
John La Rooy Avatar answered Sep 28 '22 04:09

John La Rooy


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
like image 22
NPE Avatar answered Sep 28 '22 03:09

NPE


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.

like image 36
TerryA Avatar answered Sep 28 '22 03:09

TerryA