[Tutor] programming definitions for a newbie.

Blake Winton bwinton@latte.ca
Wed Jun 18 11:40:23 2003


* Thomas CLive Richards <thomi@thomi.imail.net.nz> [030618 06:53]:
> > *object orientated programming
> A style of programming, using objects instead of...not... I've never
> really done a lot of procedural programming, so i can't really define
> this one very well...

I'll take this one, since I've been programming since the days of
procedural programming.  ;)  Seriously, you're doing procedural
programming almost every time you write a Python method.  It's
just a matter of perspective.  (There is something which I refer
to as "Pure Object Oriented Programming", where all you can do
is call methods on objects.  No "=", no "+".  Smalltalk or ML (I
think) are examples of that sort of language.)

The main difference between object oriented programming and
procedural programming is that with object oriented programming
the methods are linked to the data, whereas with procedural
programming the methods are more free-floating.

An example might help at this point.
Procedural:
>>> import string
>>> x = "aaab"
>>> string.count( x, "b" )
1
>>> string.count( x, "a" )
3

The main problem with this is that the first argument might
not be a string.

>>> y = 3
>>> string.count( y, "a" )
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "C:\PROGRA~1\Python\Lib\string.py", line 166, in count
    return s.count(*args)
AttributeError: 'int' object has no attribute 'count'

Object oriented programming, on the other hand, merges the
data with the methods that act upon them.

>>> x = "aaab"
>>> x.count( 'a' )
3

And as long as x is something that implements a "count" method,
that line will work.

>>> x = ['a','a','b','a']
>>> x.count( 'a' )
3

The thing to note is that we're calling a different "count"
method, depending on what the value of "x" is at that time.
That's the strength of object oriented programming.  It will
automatically select the correct method to call, based on
the type of the thing to the left of the ".".

Finally, there's something called "Functional Programming",
which only calls functions, but perhaps we should save that
for another time.

Clear as mud?  ;)

Later,
Blake.
-- 
 11:28am  up 33 days, 23:35,  3 users,  load average: 1.65, 1.73, 1.72