Pythonic code: Tip #3 Merging dictionaries cleanly in Python

Veröffentlicht am: 05 August 2016
auf dem Kanal: 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.


Auf dieser Seite können Sie das Online-Video Pythonic code: Tip #3 Merging dictionaries cleanly in Python mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer Michael Kennedy 05 August 2016 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 9,926 Mal angesehen und es wurde von 139 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!