Pythonic code: Tip #3 Merging dictionaries cleanly in Python

Pubblicato il: 05 agosto 2016
sul canale di: Michael Kennedy
9,926
139

Dictionaries play an absolutely central role in Python. In fact, the previous two tips both touched on dictionaries in one way or another.

This tip is about merging dictionaries.

Let's imagine we are writing a web application in Python. You'll see that your action methods will receive data from several locations. Consider a web form at the URL server.com/customer/edit/271/Fast&20apps?id=1&render_fast=True.

In there is a form with the data email = j@j.com and name = Jeff.

This data may appear in three dictionaries. One for the route, one for the query string, and one for the form data. As the web developer, answer the question where do I get the ID (which dictionary)? What if it's missing from one, where do I fall back to? This is not clean code.

We can fix it by merging the dictionaries into a master "submitted data" dictionary with the right priorities.

How do we do this merge? This video and related blog post will show you the options.


In questa pagina del sito puoi guardare il video online Pythonic code: Tip #3 Merging dictionaries cleanly in Python della durata di ore minuti seconda in buona qualità , che l'utente ha caricato Michael Kennedy 05 agosto 2016, condividi il link con amici e conoscenti, su youtube questo video è già stato visto 9,926 volte e gli è piaciuto 139 spettatori. Buona visione!