You've Been Accessing Dictionaries Wrong! #python #programming #coding

preview_player
Показать описание
This short shows you a better way of accessing Python dictionaries by using the .get() method.
Рекомендации по теме
Комментарии
Автор

Depends, defensive programming can be an anti-pattern. If the key should always exist, then you shouldn’t add defensive checks. You want your program to fail ASAP.

grantleblanc
Автор

It depends on what you wanted to do but this gives a good alternative

Marvinuni
Автор

Nice explanation for why not to use .get() on dictionaries. Thanks :)

snailedlt
Автор

This missing space before 0 kills me 💀

downsideup
Автор

Literally working on a script with nested dictionaries right now, took a small break cause I got stuck, checked my notifications and found this. I think this might be a solution to the problem I’m facing, much appreciated 🙏🏻(I knew about this method, it just didn’t cross my mind)

bassycounter
Автор

It depends on the case, there is nothing like “never use get(key) or [key]”

Sometimes you even use pop(“key”) when you need to read the value and remove it from dictionary

jadencorr
Автор

Adding on that, setDefault does the same as get, but it creates the key if it doesn’t exist

benjaminschaefer
Автор

Error is always better than the default value

praveens
Автор

Hey thx I should add the get method way more, but my solution would be in a case where i now that there can be errors I would use a try, except, else command. But get is better.

aligd
Автор

I use get... forgot about the 2nd parameter. Thanks for the reminder.

Mdroudian
Автор

if you expecting None in the value then u should use method one if not use second one

gamingland
Автор

It depends the style/case. Don't use this as a rule.

jphvnet
Автор

Это по факту правильно, для этого и нужен метод get. А [] только для записи

antoniopripizduchi
Автор

Your channel is just so useful, thank you!

pontiffmaximus
Автор

don't get it. surely th error is better?

AbdulKareemAbdulRahman
Автор

interesting, but I think I'd prefer to have it fail. Even if the subsequent code will work with a default value, if the code creating the dict did not create an item it was supposed to, then I want it to break.

Robert-qwlr
Автор

This kinda bothers me with most languages, many ways of doing the same thing, but of those options one choice is clearly better. I get that it’s due to languages getting better and maintaining compatibility, but still annoys me

swims-in-money
Автор

I use this 'get' statement before watching this video...

SohailAhmad-xktx
Автор

avoiding errors is not making your program safe. better just to check if the key is dictionary (in most cases).

maximdegi
Автор

What is the name of that type of font?

JonathanSandoval-cwpu
welcome to shbcf.ru