Newby - what is "(**dict(form.vars))"

45 views
Skip to first unread message

Chops

unread,
Mar 19, 2014, 1:06:31 AM3/19/14
to web...@googlegroups.com
I am just learning Web2py

I have an understanding of dict but now I find it used with two asterisks before it and I can't seem to find an explanation!!

Can it be used with anything other than a form? ((**dict(form.vars))?

Can I use the two asterisks on other bits of code to server a common purpose?

Thanks in advance

shapova...@gmail.com

unread,
Mar 19, 2014, 1:27:49 AM3/19/14
to web...@googlegroups.com
hi! 
Don't worry it's pretty common question for newcomers :)

This is called Function Argument Packaging and Function Argument Unpackaging.

arguments could be packaged into list or dictionary:
* - position based arguments
** - name based arguments

>>> def f(*a, **b):
return a, b
>>> x, y = f(3, 'hello', c=4, test='world')
>>> print x
(3, 'hello')
>>> print y
{'c':4, 'test':'world'}

and vice versa, arguments could be extracted for functions


>>> def f(a, b):
return a + b
>>> c = (1, 2)
>>> print f(*c)
3

>>> def f(a, b):
return a + b
>>> c = {'a':1, 'b':2}
>>> print f(**c)
3



Bruce Lamb

unread,
Mar 19, 2014, 1:38:00 AM3/19/14
to web...@googlegroups.com
Wow!! this is a bit mind bending - it will take some processing time!!

Really appreciate your VERY useful and quick response.



--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/VyouPnp0rPU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages