KEEP CALM & Use THIS In Python #code #programming #python

preview_player
Показать описание
KEEP CALM & Use THIS In Python #code #programming #python
Рекомендации по теме
Комментарии
Автор

Quite nice, but the inbuilt 'get' method achieves this already...

darrenlefcoe
Автор

It is good for our own similar classes but for `dict`, there is built-in handle for such operation:

a_dict = {"p": 1, "q": 2, "r": 3}
key = "s"
value = a_dict.get(key, f"Key: {key} does not exist.")
print(value)

The `get` method accepts optional `value` parameter to use when key does not exist.

dipeshsamrawat
Автор

Well thats exactly whats defaultdict is used for

danielglauche
Автор

You can use also that, without override the dict:
my_dict("not_exist", 0)

or

my_dict("not_exist", "Key is not exist")

seanshimon
Автор

Interesting, but doing this will get you fired

jimraynor
Автор

What is the "self" stand for?

Dragokrieger
Автор

I swear there is a dunder method for every possible case in existence

Lanxxe
Автор

What's the point of replacing the KeyError exception with your own again?
It's also not good to return a str if a key is missing. If you have any other type in the dict and try to access an object of that type which is missing, you will just run into another even more confusing exception at runtime instead of the very concise "KeyError".

It's a good way to make your life miserable later on by applying these "tricks".

Asto
Автор

Another video about a piece of functionality that while it may be “cool”, no one uses it

pittfa
visit shbcf.ru