[Edu-sig] Using objects early was Procedural front end for Zelle's graphics.py
kirby urner
kirby.urner at gmail.com
Wed Feb 7 22:07:02 CET 2007
On 2/7/07, David Reed <dreed at capital.edu> wrote:
>
>
>
> For a CS0 course that is only spending a portion of the semester on
> Python, it probably makes little difference whether you use a
> procedural or object oriented approach, but I do agree with John that
> students learn what you show them first. I think the students can learn
> to use objects just as easily as they can learn to use procedures. My
> experience of students writing their own classes also matches John's -
> the students find that much more difficult.
Note that writing one's own class needn't be the entry point. I'll talk
about
Python's "__rib__ syntax" as internal scaffolding, then introduce...
class Dog:
def __init__(self, name):
self.name = name
def __repr__(self):
return 'A dog named %s' % self.name
... saying "a dog needs a way to get born, a constructor method, and
a way to represent itself to the world, called a 'reper' in Python".
Then we'll import Dog from animals.py and instantiate a couple. Then
comes the rap about how the *blueprint* or *template* or *class definition*
is what's common to all the dog objects, but each gets a special location
in memory, to store more "personalized" data, such as a name.
In Python, that's a personalized dictionary.
>>> od1 = Dog('Rover')
>>> od1.__dict__
{'name': 'Rover'}
Even the number 1, like a dog, is an object or instance of type Integer.
Integer is like the Dog class. A given integer is like a specific dog.
>>> id(od1)
11726120
>>> id(Dog)
11794832
>>> type(1)
<type 'int'>
>>> dir(1)
['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__',
'__delattr__', '__div__', '__divmod__', '__doc__', '__float__',
'__floordiv__', '__getattribute__', '__getnewargs__', '__hash__', '__hex__',
'__init__', '__int__', '__invert__', '__long__', '__lshift__', '__mod__',
'__mul__', '__neg__', '__new__', '__nonzero__', '__oct__', '__or__',
'__pos__', '__pow__', '__radd__', '__rand__', '__rdiv__', '__rdivmod__',
'__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__',
'__rmod__', '__rmul__', '__ror__', '__rpow__', '__rrshift__', '__rshift__',
'__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__str__', '__sub__',
'__truediv__', '__xor__']
My my, look at all those __ribs__. But then, snakes have *a lot* of ribs,
don't they?
The main point being: as with any language, it's easier to develop a
reading fluency
and a recog vocabulary (of keywords, of design patterns), than it is to
develop a
writing or speaking fluency and a recall vocabulary, which is what you'll
need if they
sit you down in front of a blank screen and say "write Python code please."
I don't put beginners under that kind of pressure. It's all about
developing a *reading*
kind of fluency, with all the grammar already checked. Writing comes a bit
later,
probably involves adding a bark method to our Dog. Here too: no blank
screen at
first. Modify existing code. Just tweak existing code before you dive in
to coding
up from scratch.
I'm sure many here use similar methods. It's basic to language learning.
At one
point I could even read Arabic a little, but never could I write it at all
well. We might
have a Kalb class? (Dog?).
>>> dir(Kalb)
['__doc__', '__init__', '__module__', '__repr__']
Kirby
<http://worldgame.blogspot.com/2004/12/do-arabs-hate-dogs.html>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/edu-sig/attachments/20070207/4716c172/attachment.html
More information about the Edu-sig
mailing list