[Tutor] Dumb subclassing question

Andy Baker andy at andybak.net
Thu Aug 5 16:48:10 CEST 2004


> Rich Krauter rmkrauter at yahoo.com wrote
> Andy Baker wrote:
...<snip>
> Hi Andy,
> 
> I'd stick with classes. You might want to try composition 
> (has-a) instead of inheritance (is-a).
> One of the attributes of your MP3 object can be a path 
> object; in addition you can add all the other attributes you need.

Cheers. That gave me a whole 'Doh! Of course!' moment when I read it as I
hadn't thought of the object relationship that way round. I will have a play
with that but I am still tempted to persevere with adding methods
dynamically as it seems like it will result in the least code.

> Basic idea (untested):
> 
> import your_module # substitute name of the module you're using
> 
> class MP3(object):
>      def(__init__(self,filename):
>          self.filename = filename
>          # don't know name of class in module you reference
>          # so I'm making it up. (Next line is composition.)
>          self.pathobj = your_module.path_wrapper(filename)
> 
>          # do some work to get info out of mp3 headers
>          self.ID3Tag = self.get_ID3Tag(filename)
>          ...
> 
> if __name__ == '__main__:
>      import os
>      mp3root = '/path/to/mp3'
>      # list to hold collection of MP3 objects
>      mp3s = []
>      for root,dirs,files in os.walk(mp3root):
>          for f in files:
>              mp3s.append(MP3('%s/%s'%(root,f))
> 
>      # now you have a collection of MP3 objects
>      # and you can do stuff with it.
>      ...
>      ...
> 
> Good luck.
> 
> Rich
> 
>     * Previous message: [Tutor] Dumb Subclassing question
>     * Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
> 
> More information about the Tutor mailing list
> 



More information about the Tutor mailing list