Thursday, August 29, 2013

Python: dicts vs. classes

I like to "keep it simple stupid" when I'm coding Python. I use classes, but I don't try to shove everything into classes. When all you need is a dict or a list, use a dict or a list. However, I'm coming to think that if you have a bunch of objects that look like the following:

books = [{
  "authors": ["Neil Gaiman"],
  "title": "American Gods",
  "isbn": "9780062113450"
}]

Then it might be time to use a class rather than a dict. In this case, I'd create a class called Book, but I'd put Book instances into a simple list.

9 comments:

Kevin H said...

I'd use namedtuple for that. But maybe that's just me.

David Goodger said...

For a simple collection of data without specialized behavior, I also use namedtuple, or a "Bunch" class (see the comments for variations). But add in custom behavior (methods), and of course a class is just the ticket. (Of course, namedtuple is just a quick and dirty way to implement a minimal-behavior class. Look up its implementation in the collections module.)

The nice thing about namedtuple (and Bunch) is that when behavior needs to be added, the namedtuple definition can easily be replaced by a class definition, without affecting the rest of the code.

David Goodger said...

The advantage of a Bunch over a namedtuple is that a Bunch is mutable: you can add, remove, and modify attributes in-place. But namedtuple has a built-in order. Your choice.

Shannon Behrens said...

Thanks for the comments, guys :)

I thought about using namedtuples, but decided to just go ahead and write out a class. In retrospect, it was the right thing, but my code assumes the instances are mutable.

The Bunch class is nice, but I think have the benefit of creating a class like this is to a) give it a name b) document what attributes you expect to be in it.

It'd be nice if Python had something to let me create a simple class with a set of attributes really quickly, kind of like a case class in Scala.

Craig Maloney said...

I'd love to know how to make a list of classes searchable without having to iterate over each record in turn. Is there a way to do that easily or am I stuck with iterating over lists?

yacc said...

Actually one can just subclass from named tuple, alternatively verbose=True gives you the source to paste.

Named tuples have a number of benefits, a central one being a tuple hence immutable. Add in some properties, and methods and you get a really nice object lite with a slight functional touch (as the value cannot be changed,which makes reasoning about the code so much easier)

Art Zemon said...

If you want something that is "just a little bit" more than a dict, you can create a class derived from a dict. See UserDict http://docs.python.org/2/library/userdict.html

Shannon Behrens said...

Thanks for the great comments, yacc and Art!

Shannon Behrens said...

> I'd love to know how to make a list of classes searchable without having to iterate over each record in turn. Is there a way to do that easily or am I stuck with iterating over lists?

Do it the same way databases do it. Create indexes of some sort, either via dicts or some sort of tree.