Unpacking Argument Lists and Instantiating WTForms objects from web.py
- by Morris Cornell-Morgan
After a bit of searching, I've found that it's possible to instantiate a WTForms object in web.py using the following code:
form = my_form(**web.input())
web.input() returns a "dictionary-like" web.storage object, but without the double asterisks WTForms will raise an exception:
TypeError: formdata should be a multidict-type wrapper that supports the 'getlist' method
From the Python documentation I understand that the two asterisks are used to unpack a dictionary of named arguments. That said, I'm still a bit confused about exactly what is going on. What makes the web.storage object returned by web.input() "dictionary-like" enough that it can be unpacked by ** but not "dictionary-like" enough that it can be passed as-is to the WTForms constructor?
I know that this is an extremely basic question, but any advice to help a novice programmer would be greatly appreciated!