using dict for data attributes instead of self

Alexander Kervero vegeta.z at
Sun Oct 31 08:52:10 CET 2004

 Hi ,today i was reading diveinto python book,in chapter 5 it has a very
generic module to get file information,html,mp3s ,etc.

The code of the example is here : 

One thing that i have some doubs is this part : 

 class FileInfo(UserDict):
    "store file metadata"
    def __init__(self, filename=None):
        self["name"] = filename

So why does he wants a dictionary-like base class if he never uses any
dictionary method,except for dict.clear which is not necesary.

could be done like this?

  class FileInfo():
    "store file metadata"
    def __init__(self, filename=None):
        UserDict.__init__(self) = filename

And the FileInfo subclasses could have the specific attributes that belongs
to each file type,for example mp3.

check the link to the full example. 

The only reason is to have a base class with arbitrary attributes ? so it
can dinamicaly adquire attributes depending of its subclass or something
like that?

Is there a clearer way to do it? Is there any benefits of going this way
that i dont see? 

Maybe this is just a dummy example to just show some functionality related
to the chapter? I am a bit confused.

More information about the Python-list mailing list