Could someone please explain how this little piece of code works?
info = {}
info.update(locals())
info.pop('self', None)
info.pop('info', None)
I am assuming, and please correct me if I am wrong, but it gets all the variables in the current function and puts them in the dict and removes self and the dict it got put in, correct? Is there anything other than self and the dict I might not want going into there?
Would there be anything wrong with just JSON serializing that dict and posting it?
This probably comes from the Django template and the locals trick. The idea is to populate a number of variables inside a function, and use locals()
to pass them on to a template. It saves the effort of creating a new dictionary with all those variables.
Specifically, your code creates a dictionary of all the local variables and removes self
(the class object parameter) and info
(the variable that was just created). All other local variables are returned.
You could then JSON serialize the data, as long as the data can be serialized. DateTime variables must be converted to a string first, for example.