Tk Dynamic Menus problem

Greg Krohn ("X", "@") "gkrohnXvolucris.8m.com".replace
Wed Jul 24 21:38:47 EDT 2002


"Jeremy Bowers" <jerf at jerf.org> wrote in message
news:_mH%8.156269$uw.92148 at rwcrnsc51.ops.asp.att.net...
> On Wed, 24 Jul 2002 13:42:22 -0500, Greg Krohn wrote:
>
> > I'm not exactly sure why it's printing '3', but you do have a scoping
> > problem there.
> > Replace 'lambda: printFunc(i)' with 'lambda i=i: printFunc(i)'.
Supplying i
> > as a keyword arg for the lambda brings i into the labmda's scope.
>
>
> Thanks, that did it.
>
> I would be interested in a bit more explanation though. In the "real"
> application, "i" were instances, not numbers (if that matters).

Doesn't matter as far as I know. All varaibles in Python are an object
(instance).

> Why would
> the 'i' in the previously created lambda commands "shift" to follow the
> outside value of i?
>
> I thought I understood Python's scoping. ;-)

I thought I did, too. I wouldn't have expected the original to work at all.

<TIME-LAPSE>...Playing with code...</TIME-LAPSE>

Yeah, I can't figure it out. What version are you using? IDE?









More information about the Python-list mailing list