[Tutor] OOP clarification needed

Steve Willoughby steve at alchemy.com
Tue Jun 1 22:29:15 CEST 2010


On Tue, Jun 01, 2010 at 03:19:17PM -0500, Jim Byrnes wrote:
> def viewer(imgdir, kind=Toplevel, cols=None):
>     win = kind()
> 
> 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? 

kind is a variable.  Specifically, the second parameter passed to the viewer()
function defined here.

By saying kind() here, you are invoking it on the assumption that kind's
value is a reference to some kind of callable object.  So in theory you
could pass a function as the "kind" parameter of viewer() and that function
would get called, and its return value stored in "win".

In this case, though, the intent is for "kind" to refer to an object class
(the kind of widget this viewer is contained in or whatever).  Recalling that
object classes are themselves callable objects (specifically, calling them
is how you construct new instances of a class), "win" will end up referring
to a newly-constructed instance of whatever object class was passed as "kind".
If no "kind" parameter was given, it will default to tk.Toplevel.

-- 
Steve Willoughby    |  Using billion-dollar satellites
steve at alchemy.com   |  to hunt for Tupperware.


More information about the Tutor mailing list