The True Power of 'ChainMap' in Python

preview_player
Показать описание
In today's video we're going to be learning how ChainMap works in Python.

▶ Become job-ready with Python:

▶ Follow me on Instagram:
Рекомендации по теме
Комментарии
Автор

ChainMap - very interesting. Have to think of use cases for it, though. As in the example in this video it might be done with simple dictionaries. Look:

>>> default: dict[str, str | bool] = {
... 'theme': 'Light',
... 'language': 'English',
... 'notify': True,
... }
>>> user: dict[str, str | bool] = {
... 'theme': 'Dark',
... 'notify': False,
... }
>>> choice = default | user
>>> choice
{'theme': 'Dark', 'language': 'English', 'notify': False}

samoylov
Автор

As a only occasional python user who never heard of this, from the name I was expecting something which chains the mapping functions – i.e. given a key, it looks it up in the first dictionary, and then uses the value as the key for the second one.

PauxloE
Автор

Love hearing about all the esoteric components of the collections, itertools, functools, etc. libraries!! Thx for your reliable quality and effective communication🎉

aaronm
Автор

A case study in over-engineering in standard library. Somone was high on kool aid one afternoon.

marcusg
Автор

I have never used ChainMap. That needs to change.

astrobullivant
Автор

one application of this would probably be in writing (simple) interpreters. this could store the variables and it's values; on every block-scope start, the chain map could have a new child, and update the keys on encountering variable declaration/assignment.

JustAnotherLight
Автор

Excellent demo of theory, followed by the default/preferences example. Thanks from an old 'hobbyist'learner!

Chaplainelmo
Автор

Reading a code using Chainmap, and being written by someone else, is going to be very challenging... and probably very painful to debug.

DanielCordey
Автор

2:30 : wait, so if you try to remove the value at 'c' it will error?
Edit: 3:51 shows that yes.

drdca
Автор

4:42 I've watched over a dozen of your vids, but never noticed you were Scandinavian until you said "Sandra" in a way only a Norseman would

ultru
Автор

Looks like exactly what is possible in JavaScript with prototypical chains.
I know, Python has much more (than JS) powerful standard library, but this particular thing is exactly like prototypical inheritance.
Also it's even simpler in JS with { ...default_settings, ...settings } for one-time computation, if one doesn't need dynamic combination.

Also in generic CS terms, this is the chain of responsibility pattern.

kirillsukhomlin
Автор

Python has the most random things built into it's standard library.

haidersultan
Автор

I never knew this existed. In the past, I implemented something like this.

Chalisque
Автор

When will this be used? I can't think of any case that you want to merge dictionaries while keeping the reference to the original dictionary, just to accidentally mutate it / try to pop keys that don't exist in the first dictionary

しめい-lm
Автор

Recently i've tried to find a solution for merging default settings and user settings. The problem is nested dicts. I want to override a setting which is inside a dict inside a dict inside a dict. So I've written a recursive function which iterates over each item of the tree and updates them if needed.

winandfx
Автор

Thank you for your videos, they have titles in Portuguese, the only thing missing is AI dubbing, when will YouTube make it available globally? It would be amazing.

BuldogueTutoriais
Автор

I can't imagine that the ChainMap would be so useful. It might performs a mapping for keys and values inside dict, taking the key and iterating over each dict for values associated with this key and also by the reverse, returning one Key and all other values associated which is in the first and in any other. Instead, ChainMap stores each dict and if you want access any key this only returns the first

junioroliveira
Автор

Your example isn’t practical, because you could achieve the same by just merging the user preference inside the defaults and not doing the opposite…

TheIpicon
Автор

im curious as to why even for your example with the settings you would use this when you can do preferences = {**default_settings, **user_preferences}

averagesoul
Автор

- To all new Python coders who might be watching this: don't EVER call your dictionary "D1" or you will be fired.
- Also, he says at 4:13 that type annotations are "...just telling Python that this is of type 'ChainMap'...", but that is incorrect. Type annotations were introduced in 3.5 and Python does not care about them at all. They are solely for coders to tell other devs what type is expected. You can, however, use "__annotations__" or type conversion ("int(input("Enter an integer: "))") to create enforcement checks, flags, etc.

dirtydevotee
visit shbcf.ru