[Tutor] OOP clarification needed
Jim Byrnes
jf_byrnes at comcast.net
Wed Jun 2 17:22:00 CEST 2010
Steve Willoughby wrote:
> 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.
>
Thanks for the explanation. I didn't understand how (or why) "kind"
could change to "kind()". Sometimes I can manage to trip myself up over
the silliest things.
Regards, Jim
More information about the Tutor
mailing list