[Tutor] OOP explanation article
Alfred Milgrom
fredm@smartypantsco.com
Thu Jan 16 19:36:02 2003
Hi Michael:
I am also a recent convert to OO programmer (not completely not a
programmer - I did some programming 30 years ago, but OOP didn't exist then.)
When I tried to get my head around Classes I was really confused about the
use of 'self', and Alan Gauld (who regularly contributes to this list)
suggested that I could use other words instead of 'self', like 'this' or
'that'.
The point really being that 'self' (as I understand it) describes the
particular instance of the Class.
So think of the line
self.contents.append(element)
as reading:
thisParticularInstance.contents.append(element)
Classes (again as I understand it) cannot be called outside of specific
instances. So in your article you say you can call Basket.add(ITEM) to add
an item to your basket. This is not correct, as you also need to specify
which basket you want to add the item to. (You can tell this because the
'add' method input asks for two variables: self and item).
Instead you may have:
bicycleBasket = Basket()
briefcase = Basket()
defining two different Baskets, and then you can add things to each
different instance of the Basket class as follows:
bicycleBasket.add(helmet)
briefcase.add(notepad)
What happens is that when the program executes the code is substitutes
bicycleBasket or briefcase for the 'self' reference.
Each basket has its own list of contents.
(As an aside, you can also write the last line as Basket.add(briefcase,
notepad) but I don't think it reads as well).
Hope this helps.
Fred Milgrom
(As a final comments, perhaps you could simplify some of the code for your
description of classes. I think you could confuse beginning programmers
with constructs such as
def __init__(self,contents=None):
self.contents = contents or []
The same concepts described in your article can be explained using:
class Basket:
def __init__(self,name):
self.name = name
self.contents = []
def add(self, newItem):
self.contents = self.contents + [newItem]
def describe (self):
print self.name, "contains", self.contents
Then:
>>> briefcase = Basket('my briefcase')
>>> briefcase.add('money')
>>> briefcase.describe()
my briefcase contains ['money']
The print output is perhaps not as pretty as you may like, but it's simple
to follow the code.
At 05:29 PM 16/01/03 -0500, Michael Miller wrote:
>I want it known right now I am very much NOT a programmer. I wrote this
>as a result of a conversation I had about Object Oriented Programming
>and a lack of good explanations of what it was. It seems that every OOP
>related tutorial or article I've seen assumes that the reader already knows
>what OOP is. I'm writing this intending it to be a quick primer on what
>OOP is and what it does. You can find the file (in plain text) here
>http://onlinerock.com/musicians/blackmariah//ooptut.txt
>
>The problem is that I, as I already stated, am not a programmer. Not a good
>one, anyway. I understand all the basics of programming, but I'm at the point
>where more advanced topics such as OOP escape me. This is based on my
>understanding of OOP right now. I would appreciate it quite a bit if some
>more knowledgeable people than myself looked it over and pointed out any
>errors I may have made. Thanks.
>
>Michael Miller
>
>_______________________________________________
>Tutor maillist - Tutor@python.org
>http://mail.python.org/mailman/listinfo/tutor