[Edu-sig] Explaining Classes and Objects

Radenski, Atanas radenski at chapman.edu
Mon Jun 13 19:55:37 CEST 2005


> From: edu-sig-bounces at python.org [mailto:edu-sig-bounces at python.org]
On
> Behalf Of Kirby Urner
> Sent: Monday, June 13, 2005 9:14 AM

[... del...]
 
> The average beginner isn't going to know any programming, so why not
start
> with class/object right from the top?

The Objects First approach has gained popularity among educators and
seems to have become a sort of fashion. The idea is, indeed, to start
teaching introductory programming with objects and even classes first.
My experience is that the Objects First approach is difficult for
instructors and students alike. 

Classes are not a feasible choice to start with because they are a most
complex structure in a programming language that builds on knowledge of
virtually anything else. 

Also, starting with classes significantly delays the study of very
fundamental language elements. I have seen introductory books that
postpone the simple yet indispensable decision structures and loops
until as the second half of the book! Postponing basic control
structures for so late in a course may deprive students from having fun
with writing small yet self-contained and interesting programs. Speaking
of myself, if I, when I as a beginner programmer did not know much of
control structures until the second part on my course, I would not find
programming very interesting.

With a dynamic language as Python, it is tempting to try a Statements
First approach to introductory programming. This approach may consist of
(1) statement-by-statement execution in interactive mode of the Python
interpreter and (2) writing statements directly within a module and
executing them in file input mode. The learning benefits of interactive
programming are indisputable. However, learning how to program by
writing modules that are plain sequences of statements may be
detrimental to the student's ability to design and write well-structured
programs.

Python is a language that offers a reasonable *Middle Way* between the
Objects First and Statements First approaches. The Middle Way consists
of (1) using functions from the very beginning to encapsulate code and
structure programs, and (2) introducing control structures as early as
possible, but ONLY AS PARTS OF WELL STRUCTURED FUNCTIONS. The Middle Way
allows students to write interesting and well-structured programs early
in the course, and prepares the ground for classes and objects in the
second part of the course. 

> My approach is to start with a lot of a rule-based numeric sequences
(the
> above generates square numbers):  triangular numbers, Fibonacci
numbers,
> other polyhedral numbers.  This means writing a bunch of free-standing
> functions.

I do something similar. I start with *sets* with functions. My first
program is not hello world, but a module of two Fahrenheit-Celsius
converter functions: F2C and C2F (these are not pure functions and do
input/print). A transition from modules (that comprise functions but not
upper-level statements) to classes comes quite natural at a later stage
of the course.

> What I come to is it's easier to think of objects as something you
use, as
> primitives in the language, within procedural flows.  

Indeed, using built-in objects is simpler than defining and using
classes. Still, the concept of objects needs two underlying concepts:
variables and method (function) calls. Object variables are not so
trivial because object assignment is a reference assignment and
therefore creates aliases. Method calls require understanding of
arguments and return values. Thus, it is beneficial to study functions,
variables, and controls structures for a while, before coming to
built-in objects. 

Personally I believe that lists, dictionaries, and strings offer a great
opportunity to introduce objects. There a lot of interesting methods
that can be explored interactively and that can be used to write
interesting programs. Introducing objects with GUIs is OK, but GUI
objects seem to offer fewer opportunities for interactive exploration
than lists, dictionaries, and strings.

I cover class definitions as a last theme in my CS1 with Python course.
The continuation is CS2 with Java, an entirely class-oriented course. No
CS2 student has ever complained that classes seem unnatural or difficult
to understand. This is because, I believe, classes come at the right
time - after all underlying concepts have been well understood.

Atanas Radenski      
mailto:radenski at chapman.edu      http://www.chapman.edu/~radenski/

It takes a lot of time to be a genius, you have to sit around so much
doing nothing, really doing nothing -- Gertrude Stein



More information about the Edu-sig mailing list