Use of return locals()

1,467 views
Skip to first unread message

Andrew

unread,
Mar 28, 2012, 8:41:19 PM3/28/12
to web...@googlegroups.com
Hi ,

I've been wondering about the use of return locals() in the Forms chapter of the book, particularly with grids
def manage_users():
    grid = SQLFORM.grid(db.auth_user)
    return locals()

Elsewhere in the book use see something like this for forms return dict(form=form)
Would return dict(grid=grid) have given the same result as above?  I know a dict is required to automatically use a view with the a/c/f.html naming standard, but what does locals() use ? 
I think it would help to clarify this in the book?

Core chapter has:

Return a dictionary for a view:

def index(): return dict(key='value')

Return all local variables:

def index(): return locals()

and also this:

When an action returns a dictionary, web2py looks for a view with the name

1.
[controller]/[function].[extension]

and executes it.

Thanks
Andrew

Anthony

unread,
Mar 28, 2012, 9:31:02 PM3/28/12
to web...@googlegroups.com
locals() returns a dictionary including all the variables in the local function, so it is exactly equivalent to dict(grid=grid) in this case. See http://docs.python.org/library/functions.html#locals. In general, though, you should probably explicitly return just the variables you need to pass to the view.

Anthony

Andrew

unread,
Mar 29, 2012, 12:58:41 AM3/29/12
to web...@googlegroups.com
Thanks Anthony !
Reply all
Reply to author
Forward
0 new messages