[Tutor] OOP

Kirby Urner urnerk@qwest.net
Thu, 21 Mar 2002 08:20:14 -0800


I think this is pretty clear.  I'll interleave random
two cents just to colorize a bit, add some perspective.

At 08:08 PM 3/20/2002 -0600, Cameron Stoner wrote:
>I wanted to know if my thinking is straight about OOP, cause I'm writing 
>notes one how to
>do stuff in Python as I learn them.
>
>Here it is:
>- when making an instance(object) of a class it has all the 
>behavior(functions or methods) of that class.

Class = blueprint, template (generic cat -- none exist for real).
Instance = special case creation based on blueprint (e.g. Max).
In Python, to be an instance is to have a self.  Classes are
self-less.

>- objects take on all the characteristics of that class

But this isn't what we mean by inheritance.  Classes may
be subclasses of other classes, meaning the Cat blueprint
may inherit from the Mammal blueprint, which in turn
inherits from, say, Animal.  You might put functionality
at these various levels, "fleshing out the details"
(hyuk) as you specialize towards the species.


>- to use the characteristics you just name the behavior

Well, sort of.  You invoke a method.  An object is
characteristically a mixture of properties (attributes)
and methods (functions, behaviors).  In Python, you
can "name" a method by giving its name without
ending parentheses, in which case you get back an
indication of where this method lives in memory --
without actually invoking it.

>- Example: Say you had a cat(class) named Max(instance); who likes to play.
>     The class cat has Max as an object that can play.

That's OK.  More idiomatic is to say Max is an
instance of Cat.  Also, my Pythonic is to have
classes be uppercase (Cat) whereas instances
might not be (I sometimes prefix my object pointers
with a lowercase o, as in oCat for "object of class
Cat").

>- you have to initiate play from Max

Now we should be subtle and mention class methods.
Python is developing these, meaning we're no longer
going to need a "self" in order to trigger behavior.
Some methods may be invoked right on the class --
like having a blueprint with some circuitry active.

>- Max only plays when you tell it to

Or when some other part of your program tells it
to.  You could have a blueprint that runs off a
timer, creating random behaviors at intervals --
just adding nuance to what I think is basically an
on-target summary.

>
>Feel free to add to this or set me straight because I want to be sure on this.
>
>Thanks,
>Cameron Stoner