[Tutor] OOP clarification needed

Steven D'Aprano steve at pearwood.info
Wed Jun 2 02:03:00 CEST 2010

On Wed, 2 Jun 2010 06:19:17 am Jim Byrnes wrote:
> Whenever I teach myself a new language I have great difficulty
> understanding the nuts and bolts of it's OO implementation. Compared
> to some older procedural languages I always end up becoming confused
> by the large number of built in methods.  When reading through code
> examples I many times get hung up on trying to figure out just where
> some methods come from.
> Case in point is this code snippet from a chapter on Tkinter.
> def viewer(imgdir, kind=Toplevel, cols=None):
>      """
>      make thumb links window for an image directory:
>      one thumb button per image; use kind=Tk to show
>      in main  app window, or Frame container (pack);
>      imgfile differs per loop: must save with a default;
>      photoimage objs must be saved: erased if reclaimed;
>      """
>      win = kind()
>      win.title('Viewer: ' + imgdir)
>      thumbs = makeThumbs(imgdir)
> <snip>
> What is the relationship between kind=Toplevel in the first line and
> win=kind() further down.  Isn't "kind" a variable and "kind()" a
> method? I've probable overlooked something fundamental but  a
> explanation would be appreciated.

This has nothing to do with object oriented programming. Apart from the 
call to win.title(), there's nothing object oriented in this. The call 
to kind() is not a method but a function call, no different from (say) 
len(x) or cos(x), except that kind() takes no argument.

What you're actually getting confused by here is higher-order 

In simple languages, you have two sorts of things: code (functions and 
classes), and variables (data and instances), and they are completely 
different stuff. Functions take data as arguments, operate on the data, 
and produce new data. Here's a couple of examples from Python:

def plusone(x):
    return x+1

def plustwo(x):
    return x+2

Both functions take a single argument, which must be a number, does 
something with that number, and returns a new number.

But in higher-order programming, we realise that functions themselves 
can be data. You can write a function that takes a function as data, 
operates on the function, and produces something new. Here's a trivial 

def print_table(func, start, stop):
    print '  x  =',  # Note the comma.
    results = []
    for x in range(start, stop):
        print x,
        result = func(x)
    print 'f(x) =',
    for item in results:
        print item,

You then call this like:

print_table(plustwo, 1, 7)

and you get something like this:

  x  = 1 2 3 4 5 6
f(x) = 3 4 5 6 7 8

In the example you give, you have an argument named "kind". It is 
expected to be some sort of function or class, and gets the default 
value of TopLevel if not supplied. In the body of the function, this 
function or class is called, to produce a value which is then 
named "win". Judging by the default value and the name of the inner 
variable, I would say it is expected to produce a window object, so any 
function or class that returns a window object will be suitable.

Steven D'Aprano

More information about the Tutor mailing list