1

I am working with a finite list of "objects" which I will be using in my small script:

books = [ 
    {'name': 'Book1': 'id', '123', 'text', '.....'},
    {'name': 'Book2': 'id', '123', 'text', '.....'},
    {'name': 'Book3': 'id', '123', 'text', '.....'},
]

Does it make sense to create a class for this book type, or is it fine the way it is?

The problem with creating a class is that there would be slightly more boiler-plate.

TheOne
  • 10,819
  • 20
  • 81
  • 119

4 Answers4

4

Since you simple want to store data, I would suggest that you use named tuple. This is a great example of how named tuples are used.

Ideally, you could simple loop through the list of books, and then take the values of each list, and then make place them into a named tuple. Then one could simple access them like so:

some_book.name

Here, is a short example:

>>> from collections import namedtuple
>>> NetworkAddress = namedtuple('NetworkAddress',['hostname','port'])
>>> a = NetworkAddress('www.python.org',80)
>>> a.hostname
'www.python.org'
>>> a.port
80
>>> host, port = a
>>> len(a)
2
>>> type(a)
<class '_ _main_ _.NetworkAddress'>
>>> isinstance(a, tuple)
True
>>>

This would make accessing the data in your book a lot simpler as well as help you make sure that all the data necessary for the book is present.

Community
  • 1
  • 1
Games Brainiac
  • 80,178
  • 33
  • 141
  • 199
2

Your current code makes perfect sense the way it is. And it is MUCH faster in runtime since Python is somewhat slow with instantiating objects that are not built-in. If you want high performance then avoid instantiating classes in Python as much as possible. However you pay the price of your code being somewhat ugly.

You can even make a function which adds entries to your list of books. That would be equivalent of your "constructor" if you had a class.

Shashank
  • 13,713
  • 5
  • 37
  • 63
2

If you've many "dumb" objects with just members and no methods then this pattern may be useful:

class Obj(object):
    def __init__(self, **kwargs):
        self.__dict__.update(kwargs)

after those three lines you can create objects with the fields you need simply with

books = [
   Obj(name='Book1', id='123', text='.....'),
   Obj(name='Book2', id='124', text='.....'),
   Obj(name='Book3', id='125', text='.....'),
   Obj(name='Book4', id='126', text='.....')
]

and you can use them later with

for b in books:
    print b.id, b.name

This approach will allow you to write the code of the program using the same more readable syntax that will be required anyway if in future you will need to update to full-blown specific classes with methods.

6502
  • 112,025
  • 15
  • 165
  • 265
  • Great answer, but it seems like a namedtuple requires less boilerplate--perhaps with a bit less flexibility. – TheOne Sep 24 '13 at 15:12
  • @Ramin: namedtuple is great when you want to use its main feature... i.e. that instances are also tuples. However namedtuple definition is explicit, and in change you get a type. This is more "dynamic" and all those objects are just "objects" (no specific type) in which each instance can have different members (a la Javascript). It can be good or a nightmare, depending on what you need to do. – 6502 Sep 24 '13 at 15:27
0

If you don't plan on ever using methods you should be fine like that. Otherwise I would recommed using a class.

Wurstbro
  • 974
  • 1
  • 9
  • 21