[Tutor] OOP - have I done it right or could it be better?

Kent Johnson kent37 at tds.net
Tue Jan 23 21:33:39 CET 2007


Original Brownster wrote:
> Hi,
> I'm fairly new to using OOP and very new to Python, my first program is
> coming along well and I have split the code into 2 modules.
> 
> The program finds stream urls, downloads them, re-encodes them to othe
> formats, all this functionality I have created in one module with one
> class called Streamrip and methods to handle this.
> The second module contains all the code to handle the pygtk interface
> and written as a separate class called Windowapp()
> 
> You call the Windowapp() routine, in its' init method I have a call to
> create an instance of the streamrip class:
> 
> self.UC = Streamrip(var=....)
> 
> from then on within the Windowapp class i refer to the methods in
> streamrip like
> 
> self.UC.Dumpstream(...)  etc.

This all sounds good. You have a module, Streamrip, that implements the 
core functionality you need. From your description, this module doesn't 
know about the GUI so it could be reused by another kind of client, 
tested, etc. You also have a module that implements a GUI front-end to 
Streamrip. All good. Just make sure the Streamrip module doesn't know 
anything about the Windowapp module - keep the dependencies one-way.

> I've got this far and now I am thinking all though it works would it be
> better another way for example should I have used inheritence and
> allowed the windowapp class to inherit the methods of streamrip()? 

No, that is not better. Windowapp is not a kind of streamrip (which is 
what an inheritance relationship implies), it is a client of streamrip.

Kent



More information about the Tutor mailing list