>
> for fieldname in db.mytab.fields:

      db.mytab.fieldname.represent = lambda x,row:row[fieldname]
>

Two problems above. First, since "fieldname" is a variable, you cannot do 
db.mytab.fieldname -- instead it should be db.mytab[fieldname]. Second, in

lambda x, row: row[fieldname]

the "fieldname" variable is defined in the scope outside the lambda 
function. The last value held by "fieldname" will be the name of the last 
field in the loop. The lambda functions won't actually be called until 
after the loop has finished, so all of them will end up referring to the 
fieldname of the last field. To get around this, you can pass in the 
fieldname as a default argument to lambda:

lambda x, row, f=fieldname: row[f]

To make things even a bit simpler, you can take advantage of the fact that 
you can iterate over the fields directly rather than their names (and you 
can access each field's name via its "name" attribute):

for field in db.mytab:
    field.represent = lambda x, row, f=field.name: row[f]

Anthony

-- 

--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to