[Tutor] LCM revisited + OOP

wrw at mac.com wrw at mac.com
Wed Nov 28 02:52:34 CET 2012


On Nov 27, 2012, at 8:13 PM, Ray Jones <crawlzone at gmail.com> wrote:

> 
> Part I
> I am a good way through MIT's Introduction to Computer Science and
> Programming as offered through edX. I'm not certain I'm going to pass
> the course this first time through, the major hangup being the
> understanding of OOP.
> 
> Part II
> When the LCM thread came through, I wrote some quick code do the
> computation. I began by creating a function that generates a list of
> primes and reversing the list high to low. Then I created a recursive
> function to find the prime factors for each of the numbers and
> manipulated the results to the LCM.
> 
> What do these two things have to do with each other? I can see some of
> the vast power of OOP. Unfortunately I can't see when or why I would
> create objects in my everyday programming. So I went back to look at my
> LCM code to see if there a way to create better code by using OOP rather
> than the functions I used. I can't see a better way. I know I can use
> OOP - I just don't see how setting it up with a couple of classes would
> make it simpler or better than the non-OOP way.
> 
> I'm not including code because I'm more interested in understanding the
> whys and wherefores of using OOP vs. not using it than in receiving help
> with my specific code. (And I'm not a particularly fine coder to begin
> with... ;) ).
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

Ray,
  I'll be VERY interested to see how others answer this question, because I'm still working through it myself.  I started programming back in the days of FORTRAN IV, writing data analysis programs in support of my own work.  I've moved forward since and have spent the last two years learning Python.  All my initial development in Python was very much procedural and I've been quite happy to leverage its power.  More recently, as I decided to start developing simple GUI front ends for some of my stuff, I took another long hard look at the sections in my various books on OOP, and have finally (I hope) started to see daylight.  To be overly simplistic, OOP really shows its worth when one is creating classes that _do_ represent objects or abstractions of objects (and of course, that's why GUI widgets lend themselves to nicely to OOP).  To give a slightly more concrete example, I develop a lot of network utilities, and it suddenly became obvious that a "network" class would be a very useful thing.  That class allows me to define a network object in terms of a remote end-point, and then perform all sorts of actions (tests) on the "network" between here and there.  Behind the scenes, that class can share data among its internal modules and functions, but each instance of the class is completely unique and the fact that the collection of "hops" that make up that particular "network" is also unique can be safely hidden (or exposed) as needed.
  So, to summarize - for a lot of purely procedural programming - there may well NOT be any advantage to OOP, but when you start developing code that needs to mimic/represent or model complicated "objects" then OOP starts to shine.  Done right it simplifies the interface to the object, simplifies future debugging when objects are combined, and provides structure that helps you get the "logic" right.

Good luck that class,
Bill


More information about the Tutor mailing list