Avoid endless if-else statements with this Python Tip!!

preview_player
Показать описание
Avoid endless if-else statements with this Python Tip!

📓 ML Notebooks available on Patreon:

If you enjoyed this video, please subscribe to the channel:

~~~~~~~~~~~~~~~ CONNECT ~~~~~~~~~~~~~~~

~~~~~~~~~~~~~~ SUPPORT ME ~~~~~~~~~~~~~~

#Python #Shorts
Рекомендации по теме
Комментарии
Автор

Please don't do this in production. It's much less clear than an if/else. clarity > consiseness

ruairiclinton
Автор

this dude legitimately gives the worst advice for programmers

userou-igze
Автор

Tired of ifs? Replace a list with a long ass line for the same ammount for worst redability

imJeNaiKe
Автор

“Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.[...]” -The Zen of Python

micaelmz
Автор

Oh cool, that’s a great tip!
*reads comments
This is why I’m not a programmer

zachary
Автор

I'm a beginner and this dude makes everything so much more complicated 😅

weeby_guy
Автор

Switch statement: let me introduce myself

Python: We don't do that here

tremon
Автор

Obfuscating something that was readable

flareflo
Автор

He woke up and said to himself, “Let’s create garbage “.

qazwsx
Автор

less code doesn't always mean better, more readable code. if its not broke, dont fix it. you just turned a simple if statement into a mess that someone will have to google to figure out. I'm frustrated as I watch this because I have to deal with code like this where people like to monkey around with stuff thats working fine by turning it into less code that, to them is more readable, but for everyone else it requires you to either google what they're trying to do sit there staring at it for 20 minutes. PLEASE DON'T DO SOMETHING LIKE THIS IN PRODUCTION

JoeG
Автор

I've used this before to pick between different procedures given an input. It's very helpful, but situational at best.
You can make it more readable by properly formatting the dictionary, so it's clear it's a case of input: function.

sanketower
Автор

Great advice, make it as hard as possible so when your company kicks you they will have no clue what this does

requestfx
Автор

Why not use pattern matching with match case statements? They're far more readable and have same, if not less time complexity...

professornumbskull
Автор

Only do this for literal values (jump table) this can become problematic if you have different args for functions

fallingintime
Автор

This is less readable in my opinion and function-calls are much expensive, why should we do that? :)

tutorexilius
Автор

I believe code should be beautifully written so that other programmer can read and understand it immediately

SantoshKumar-smpc
Автор

Fun fact : Python 3.10 has match-case statements that make this job easier

aditmagotra
Автор

There is a lot of hate on this, but the thing itself is pretty useful, just a little bit different.

Instead of using get, use indexing to call function pointers in the dictionary. Like for example "func_dict["+"]" which could redirect to a function pointer key called + which correlates to the "add" function. This is actually very helpful and very close to what is being done in C. The hate on this approach, though not quite correct shown here, is NOT correct. Also because Python is an interpreted language, if else chains won't get rolled up by a compiler, so it's actually more performance heavy to use them, than a single dict object reference by a lot

skylo
Автор

I've been searching something like this every once in a while.
Glad you showed a nice way to do this.

LordTeleman
Автор

From what I can tell this is beneficial for time complexity, going from linear when running through a potentially long conditional rather than a constant time when checking a dictionary object(map). It does increase space complexity but it depends on what is needed.

Soppybobs
welcome to shbcf.ru