On Tuesday 04 December 2007 23:16:44 aaronp wrote:
> Hi All:
>
> I'm new to turbogears and have a question about dynamic columns in a
> DataGrid widget. I'm adding a column for each instance in a list, and
> I'm trying to figure out how to determine what column I'm in when
> executing the common lamba/getter. When creating the column fields
> list, I can pass in the column or id of the object, but since the
> lambda is actually executed after the loop has completed, I get the
> last value (after the full loop has executed) rather than the intended
> value (the value created in the loop when the lambda was created)
> passed into the expression.
This is a typical side-effect of the way python lexical scoping works.
You need to create a lambda-local reference to the row, which is most easily
done like this:
for row in rows:
row_getters.append(lambda row=row: work_with_row(row))
Notice the row=row-part - it will make row a keyword argument with a
default-value which is evaluated(!) at lambda creation time.
Diez
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---