Use A Dictionary Instead Of If-Else Statements // Python Tips

preview_player
Показать описание


🎓 Courses:

👍 If you enjoyed this content, give this video a like. If you want to watch more of my upcoming videos, consider subscribing to my channel!

👀 Code reviewers:
- Yoriz
- Ryan Laursen
- Sybren A. Stüvel
- Dale Hagglund

#arjancodes #softwaredesign #python

DISCLAIMER - The links in this description might be affiliate links. If you purchase a product or service through one of those links, I may receive a small commission. There is no additional charge to you. Thanks for supporting my channel so I can continue to provide you with free content each week!
Рекомендации по теме
Комментарии
Автор

Notice the general principle here: replace repetitive code with a simpler sequence of code that keys off a data structure. So adding new cases to deal with involves adding new entries to the data structure, not adding more repetitions of the code sequence.

This is called “data-driven programming” or “table-driven programming”. A very good technique for writing code that is shorter and easier to maintain. Because the less code you have, the less work you have to do to maintain it, and the less chance there is for bugs to creep in.

lawrencedoliveiro
Автор

Brilliant! I discovered your channel yesterday and have watched around 4 Python videos. I love your videos because they are not the usual beginner tutorials, but are more geared towards intermediate-advanced programmers who want to keep improving their Python skills. Thanks for the videos, and keep them coming!

josewilhelm
Автор

I use this all the time. I LOVE it. Make sure you either put this inside a try catch or another way to raise a specific Exception

JonMartins
Автор

This guy is amazing. I'd love to see him expand! Perhaps Arjan has a twin who knows rust?

basedmuslimbooks
Автор

I absolutely love these shorts; honestly. I've never ticked the bell icon on YouTube in my life but you provide so much value I'd be stupid not to! Thanks Arjan!

MrAjiii
Автор

Love your in-depth videos and also how you're utilizing the youtube shorts format, keep it up! By chance I was reading up on this before I saw your video and learned that python 3.10 introduces match-case, seems like a good alternative as well.

CorvusKrOw
Автор

Arjan i love these short videos, are amazing! Can you please explain some logging good practices?

miguelvasquez
Автор

Another advice.
Don't call directly the dictionary output. Store the output in a variable, called buy_strategy, for example, and call that buy_strategy().
That way it's more readable and it also allows you to catch errors in case the dictionary doesn't return a valid output.

sanketower
Автор

Im only 7 months into learning to program but I just helped someone last week reduce their roshambo game from 50+ lines down to about 15. This was a big thanks to doing something very similar with dict

jamaka_me_code
Автор

This is such a convenient sorting option, I still can't believe I ended up doing it just by looking for ways to make the code more readable.
I love Python man

DJ-hioc
Автор

Sir Arjan, this is exactly what I was searching for. Thank you so much!!!

MichaelSchellerwayne
Автор

Thank you for the idea! That's even more like CASE operator which people complain missing in Python.

slasaru
Автор

Great vid. Reminds me of the advice in Clean Code to use polymorphism in place of if / else, I think it's the same principle.

jannicholzer
Автор

How do you pass the other parameters to the functions, which number and types may vary between the functions? Do you use extra param dicts or lists that you expand with *args and **kwds?
Thank you very much for your nice, little tutorials.

skyletwings
Автор

I dunno if this is necessarily a recommended pattern, but I like to put all the functions into a seperate module, then I can create this dictionary dynamically like so:

import my_functions # where the functions are
import inspect

func_dict = inspect.getmembers(my_functions, inspect.isfunction)

I can then call a function by it's declared name as the key. This saves me the manual adding of the functions to the dict, I can just put functions in that module and it always works.

MattRose
Автор

I first saw this pattern in an early introduction to using IoC in JUnit. Great for text based games where commands are used so heavily.

budoray
Автор

nice tip! I used this pattern to reference classes, where each class is an implementation of an API and offers a bunch of functions/method and has a dict with attributes for various specification constants like headers, endpoints etc. I thought it would be better to put them in kind of a namespace/class/structure instead of just in one file...

multigladiator
Автор

Do not parametrize functions with strings! Use enums

nikolaychechulin
Автор

I think I used this technique (or something very similar) on the last Advent of Code. Felt like I found out a new cool way of doing things...

kruvik
Автор

Very useful Arjan. Is it possible to use this trick if the conditional has greater than or less than?

ladycoder