6

I enjoy all the python libraries for scraping websites and I am experimenting with BeautifulSoup and IMDB just for fun.

As I come from Java, I have some Java-practices incorporated into my programming styles. I am trying to get the info of a certain movie, I can either create a Movie class or just use a dictionary with keys for the attributes.

My question is, should I just use dictionaries when a class will only contain data and perhaps almost no behaviour? In other languages creating a type will help you enforce certain restrictions and because of type checks the IDE will help you program, this is not always the case in python, so what should I do?

Should I resort to creating a class only when there's both, behaviour and data? Or create a movie class even though it'll probably be just a data container?

This all depends on your model, in this particular case either one is fine but I'm wondering about what's a good practice.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
arg20
  • 4,893
  • 1
  • 50
  • 74
  • Adding info to your question: A dictionary is mutable and is a container type which can store any number of python objects or other container type. You are trying to compare a container and an object! What's better? I don't know too. – Prasanth Oct 06 '12 at 15:24
  • Similar to: http://stackoverflow.com/questions/4045161/python-should-i-use-a-class-or-dictionary – arg20 Oct 06 '12 at 16:33

4 Answers4

6

It's fine to use a class just to store attributes. You may also wish to use a namedtuple instead

The main differences between dict and class are the way you access the attributes [] vs . and inheritence.

instance.__dict__ is just a dict after all

You can even just use a single class for all of those types of objects if you wish

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

movie = Bunch(title='foo', director='bar', ...)
Lukas Graf
  • 30,317
  • 8
  • 77
  • 92
John La Rooy
  • 295,403
  • 53
  • 369
  • 502
  • 4
    On the other hand, it's also fine (and much easier for everyone involved) to use existing data structures to hold data whenever appropriate. Also, `namedtuple` is awesome but some use cases don't get along with its immutability. –  Oct 06 '12 at 15:20
0

In your case you could use a class that inherits from dict (e.g class MyClass(dict)) so that you can define custom behavior to your dict-like class or use UserDict.

thikonom
  • 4,219
  • 3
  • 26
  • 30
0

It depends on what you really mean for "perhaps almost no behaviour", if dict already provides what you need stay with it. Otherwise consider to subclass dict adding your specific behaviour. Since Python 2.2 it is possible. Using UserDict is an older approach to the problem.

You could also use a plain dictionary and implement the behaviour externally via some function. I use this approach for prototyping, and eventually refactor the code later to make it Object Oriented (generally more scalable).

You can see what a dictionary offers typing this at the interpreter:

>>> help({})

or referring to the docs.

Paolo
  • 20,112
  • 21
  • 72
  • 113
-3

I would stick to KISS (Keep it simple stupid). If you only want to store values you are better off with a dictionary, because you can dynamically add values at runtime. WRONG:(But you can not add new filds to a class at runtime.)

So classes are useful if they provide state and behaviour.

EDIT: You can add fields to classes in python.

Puckl
  • 731
  • 5
  • 19