[Tutor] teaching for purpose

avi.e.gross at gmail.com avi.e.gross at gmail.com
Mon Jul 25 14:23:54 EDT 2022


The recent discussion inspires me to open a new thread.

 

It is about teaching a topic like python or kinds of programming it supports
and doing it for a purpose in a way that does not overwhelm students but
helps them understand the motivation and use of features.

 

The recent topic was OOP and my thought was in several directions.

 

One was bottom-up. Show what the world was like when you had individual
variables only (meaning memory locations of sorts) and then some sort of
array data structure was added so you can ask for var[5] and then if you
wanted to store info about a group of say employees, you had one array for
each attribute like name, salary, age. So a user could be identified as
name[5] and salary[5] and age[5].

 

Then show what a pain it was to say fire a user or move them elsewhere
without remembering to make changes all over the place.

 

So you show the invention of structures like the C struct and how they now
hold an employee together. Then you show how functions that operate on
structs had to know what it was in order to be effective and general and
that enhancing a struct to contain additional member functions and other
features like inheritance, could lead to a concept where an object was
central. Yes, there are more rungs on this ladder but this in enough for my
purpose.

 

The other way is to work more from top-down. Show them some complex network
they all use and say this could not easily be build all at once, or
efficiently, if it was not seen as parts and then parts within parts, with
some parts re-used as needed. At the level of the programming language, the
parts can be made somewhat smart and with goals if designed well. If you
want to be able to process a container holding many kinds of parts, you may
want each part to know how to do several things to itself so if you want all
parts to be able to print some version of what they contain, you want to say
object.printYourSelf() and each will do it their own way. You may want to
invent protocols and insist that any object that wishes to be considered a
member of this protocol, must implement the methods needed by the protocol
and thus you can pass a collection of such objects to anything that wants
the protocol to be used without worrying about errors if the wrong kind of
object is there too.

 

Again, I hope you get the idea. You can lead up to objects from basics or
lead down as you deconstruct. Of course there are many other ways.

 

I mention this because I personally find some features almost meaningless
without the right context. Consider any GUI such as a browser where events
seem to happen asynchronously depending on movements of the mouse, clicks,
key-presses, and even events coming in from outside. Various parts of the
program are not RUN in a deterministic manner but must pop into being, do a
few things, and go away till needed again. In a sense, you set event
listeners and attach bits of code such as functions or perhaps a method call
for any object. In such an environment, all kinds of things get interesting
and choices for implementation that do things in what may seem odd or
indirect ways, suddenly may make some sense.

 

So imagine first building an almost empty object that holds nothing and has
a very few methods. Why would anyone want this? A while later you show how
to make one and then more objects that inherit from the same object but
BECAUSE they share a base, they have something in common, including in some
languages the ability to be in a collection that insists everything be the
SAME. Then you show how an object can be made easier and faster if it is
only a bit different than another or by combining several others.

 

At some point you want to make sure the students get the ideas behind it,
not just some silly localized syntax useful only in that language.

 

Once you have an idea of purpose, you may show them that others had similar
purposes in mind but chose other ideas. Take the five or more ways python
allows formatting text. As soon as you realize they are all variants with
some similarities in purpose, you can more easily classify them and not
struggle so much as to why anyone would do this. At least that is how my
mind works.

 

What I find helpful in motivating using objects when they really are NOT
NEEDED is when it is explained that part of the purpose was to make a set of
tools that work well together. Modules in python like sklearn stopped
looking weird after I got that. I mean you can easily make functions for
yourself (well, maybe not easily) that implement taking some data and return
N clusters that are mostly closer within each cluster to some centroid than
to other clusters. 

 

So why ask the user to set a variable to a new object of some sort, then
repeatably ask the object to do things to itself like accept data or add to
existing data, set various internal attributes like values telling it to be
fast or accurate or which technique to use, or to transform the data by
normalizing it some way, and run analyses and supply aspects of what it came
up with or predict now new data would be transformed by the internal model?
This does not, at first, seem necessary or at all useful.

 

But consider how this scales up if say you want to analyze many files of
data and do some comparisons. Each file can be held by it's own object and
the objects can be kept in something like a list or matrix  and can be used
almost interchangeably with other objects that implement very different ways
to analyze the data if they all take the same overall set of commands, as
appropriate. All may be called, say, with new data, and asked to predict
results based on previous learning steps. 

 

The point in all the above, which may easily be dismissed by the volume, is
that I think part of learning is a mix of ideas in the abstract as well as
some really concrete programming along the lines of prototyping. Learning
Object Oriented Programming in the abstract may seem like a good idea,
albeit implementations vary greatly. But knowing WHY some people developed
those ideas and what they were meant to improve or handle, .

 

I shudder sometimes when a functional programming person tries to sell me on
using closures to retain values and so on, until I realize that in a sense,
it overlaps object-oriented programming. Or does it?

 



More information about the Tutor mailing list