views:

381

answers:

5

First, I'd like to point out that I know OOP concepts and understand the differences between dictionaries and classes. My question is about what makes sense design wise in this case:

I am designing a webapp in python and I have to represent something like a book object. Books have chapters and chapters have titles and content. For simplicity, lets say that the content is plain text.

My question is, should I make the book and chapter classes or dictionaries? I know it'd look neater to use book.chapter instead of book['chapter'], and if I end up having methods in the future, it might make sense to puts them in the book class. However, I'd like to know if there is any overhead to using classes instead of storing the information in dictionaries?

If I don't want to instantiate a book object from a database everytime and store it as a pickle, I'd have to be worried about incompatibility with past book objects if I add/remove data members from a class.I feel that it'd be easier to deal with this problem in dictionaries. Any pointers on whether/when it makes sense to use dictionaries instead of classes?

+3  A: 

Objects were originally created to bundle data with functionality. If you just want to store data, use dictionaries. If you want to include methods for manipulating the data, use objects.

mcandre
I think the first sentence is a myth.
Svante
@Svante, If it is a myth, then what is the truth?
David Locke
No, I read it in a C book that traced the history of data types.
mcandre
@Svante: Disagree: the object model is designed to couple attributes and behavior. http://en.wikipedia.org/wiki/Object-oriented_programming#Fundamental_concepts
S.Lott
OK, I guess I took the word "bundle" too serious. "couple" is much better.
Svante
+6  A: 

A few thoughts:

  1. If you start with a dictionary, you can always switch to a custom class later that implements the mapping protocol (or subclasses dict). So it's probably a good starting point.

  2. You can define custom Python objects to use __slots__, which will be faster and more memory efficient if you have a large number of objects.

  3. If you use a custom Python object, it will be easier to replace it in the future with an object written in C. (I've never tried it, but I would expect that subclassing dict from C would be a tricky proposition.)

Daniel Pryden
+1 for `__slots__`
Hank Gay
+1  A: 

Kind of similar to your problem: http://stackoverflow.com/questions/794132/returning-an-object-vs-returning-a-tuple

There's no clearcut choice. Dictionaries are fast, nice, and clean. Class instances are, after all, a special dictionary. If you want to provide a way to add functionalities and change your interface later on, a class would be the best choice, but overdesign something simple is always a bad choice. Python is not java, where everything must be an instance of a properly made class. This is a sin I fall for as well.

Stefano Borini
+1  A: 

If you go with objects, I wouldn't store the data pickled in the database simply for the reasons you gave. It would be considerably worse if you underwent a change of language or similar.

FWIW, I would start with a dictionary. If things get complicated or new features are needed, make it an object.

Corey D
Raw dicts are much easier to dump to JSON, exchange with other programs and the like. They require no custom class definitions (other than a description of the nesting convention, which here is obvious).
Gregg Lind
+6  A: 

This is highly unlikely to be the bottleneck in your application, so worrying about the performance is probably a waste of time. However, if this is the bottleneck or you just have some time on your hands, look into using a namedtuple. It combines the immutability and low memory footprint of a tuple with the nice syntax of class attributes.

Hank Gay
+1 for mentioning namedtuple. If you're using Python >= 2.6, it's definitely a good choice to look into.
Daniel Pryden
+1: It's not "probably a waste of time". It's a waste of time. All the web frameworks use classes. Just use classes.
S.Lott
S. Lott, what does "All the web frameworks use classes" have to do with this? Are you suggesting the web frameworks are guides for best bare-metal performance? Or are you suggesting, they're "fast enough", even with the (very small) overhead of classes.
Gregg Lind