[Tutor] Whats so good about OOP ?

Bill Kranec billk at fastmail.fm
Sat Mar 12 15:24:21 CET 2005

Hi Mark,

In my brief experience with OOP, I would say that the main advantage is
organization of code.  All functions and attributes that you need to work
with an object are wrapped up nicely inside the object.  Also, object
inheritance provides a great framework for easy customization.

For example, to write some code to model a car, you might use an object:

class car:
   def __init__( self, name ):
      self.name = name
      self.numberOfDoors = 4
      self.milesPerGallon = 25
   def drive( self, time ):
      print self.name+' drove for '+str(time)+' hours, and used '\
          +str(self.milesPerGallon*time)+' gallons of gas.'

In this example, the drive function will only be accessible to things that
we have defined as cars.  You can also have specialized versions of the
car class (for example, to model a Porsche) which have different
attributes ( self.milesPerGallon = 15 ), but have the same methods (you
still want the drive() method).

Note that this task isn't necessarily easier to do with OOP, (you could
just as easily define a function which takes all of the attributes as
inputs), but I believe the structure is clearer, and you get the added
bonus of expandability.

I hope my example has been helpful, and that someone here will correct me
if I've said something wrong.  Good luck using OOP!


On Sat, 12 Mar 2005, Mark Kels wrote:

> Hi list !
> I want to know whats so great in OOP...
> I have learned some of it, but I don't understand why everybody like
> it so much...
> Can anyone give me an example for a task that could be done only with
> OOP or will be much simpler to do with it ?
> Thanks in advance.
> --
> 1. The day Microsoft makes something that doesn't suck is probably the
> day they start making vacuum cleaners.
> 2. Unix is user friendly - it's just picky about it's friends.
> 3. Documentation is like sex: when it is good, it is very, very good.
> And when it is bad, it is better than nothing. - Dick Brandon
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor

More information about the Tutor mailing list