[Tutor] Dumb Subclassing question

Alan Gauld alan.gauld at blueyonder.co.uk
Thu Aug 5 15:01:06 CEST 2004


> I, like everyone else in the universe, am writing a prog to
manipulate and
> organize mp3's...

Nope, I just use iTunes :-)

> So I subclass the 'path' object as 'mp3path' and add some of my own
methods.

Sounds OK.

> I have added a new method ID3tag to my MP3path class
> I want myMP3path.files to return a list of objects that can still
access my
> ID3tag method.

In that case they need to have access to objects of your class.
Or do you mean you want other objects to access the ID3tag method
of the returned objects? ie the returned objects should have ID3tag
methods?

> Options that have occurred to me:
>
> 1. Override every method in 'path' that returns paths with a wrapper
method
> that converts them into mp3paths. This seems ugly, boring and
pointless.

Its the only sensible way if you want all path objects to be your
specialised variants. How else can the path objects be treated as
your objects unless they get converted at some stage? But see below...

> 2. Forget subclassing and add methods directly into the path module
source.
> This will be bad when a new version of 'path' comes along and also
seems
> like cheating.

It will break all sorts of things, especially if you try using path
for something where you really want path objects! Or more likely you
try to use a 3rd party bit of code that expects real path objects...

> 3. Add methods dynamically into the path object. (Is this
'decorator'?)

No its not decorator and is even more messy!

> looked at the instancemethod function in the standard library 'new'
module
> and this adds methiods to instances but not to classes so I would
have to do
> this for every instance.

Exactly so. Yuk!

> 4. Forget the whole OO thang and just use functions. (Looking more
> attractive by the minute ;-)

How would that help? You still have the same issues?
One thing you might like to consider is the composite pattern.
Here we distinguish between link nodes and leafnodes of a tree.
You only want path nodes to gave ID3tags methods if they are
leaf nodes(ie files) I assume?

So you can write a function that determines the nature of
path node you get and only convert the leaf nodes to your
class type. That is instead of overriding path to retirn
your objects why not get the list of paths back and then
convert those as needed to your class?

Does that make sense - I don't think I'm explaining it
very well... And don't have time for an example!
Basically write a type conversion function (like int(),
str(),list() etc) and apply it as needed.

Pseudo code:

def mp3path(aVanillaPath):
    mp3p = MP3Path()     # create new instance
    for attribute in aVanillaPath:  # use getattr()???
        mp3p.attribute = attribute   # copy inherited stuff
    return mp3p

for path in myPath.files():
    if path is leaf node  # using isinstance or somesuch?
       path = mp3path(path)
       path.ID3tag()      # do things with new path object
HTH

Alan G.




More information about the Tutor mailing list