How To SOLVE Indentation HELL In Python

preview_player
Показать описание
How to solve indentation hell in Python. #Python #Shorts
Рекомендации по теме
Комментарии
Автор

If you're checking if all conditions are true, you can use the all() function as well like so:

if all([has_money, has_time, has_friends, has_internet]): 
print("yay!")
else:
print("sadge")

neocodes_
Автор

this has to be one of the worst programming shorts I've seen. A terrible example of an over-discussed topic. the "log" function to save 2 characters of typing is the cherry on top.

usrnm
Автор

The worst about videos like this is that they present something secondary, such as deep indentation or other visual things as a problem, instead of talking about the underlying logic, which is what _really_ matters. This is not an aesthetic matter of readable style, but a hard objective matter of correct logic. Sometimes you just need complicated logic, and making it pretty does no good if it isn't correct.

lhpl
Автор

Is no one going to talk about the log function implementation?

Dev-Siri
Автор

or.... You could just use simple if/elif/else

if not has_money:
log('No money ...')
elif not has_time:
log('No time...')
elif not has_friends:
log('No friends...')
elif not has_internet:
log('No internet ...')
else:
print('Yay!!')

techiza
Автор

The AND operator in the corner crying:

UnkownUnkown
Автор

It's easier for experienced python coders, but not for beginners. P.s they'd just use the "and" operator

yourbigfan
Автор

I don't like to keep a lot of guard clauses, although I've done this a lot in my own code, once I wrote at my job I realised a lot of code actually doesn't depend on the occasional conditional branching. For example,


if has_camera():
print("Taking pictures")
else:
print("Connect camera")

# some code....


To execute some code you need to ensure you don't have an early return or something, and even if there's some code that shouldn't run for now, in the future it might need to run for that function, regardless of the condition

So this kind of nesting is acceptable, in some scenarios

k_gold
Автор

“But we’ve already shipped this code it’s implemented in everything from cameras to weapon guiding systems. Recalling it all will cost millions, if not billions, is that really worth it for two lines of code?”

“YES HE SAID AT ALL COST!”

Tobias
Автор

I like your lambda. Before I got comfortable with logging, I used to do something similar, just not as elegant.

deemon
Автор

You don't even need return

log = lambda *args: print(*args)
class i:
has_money = True
has_time = True
has_friends = False
has_internet = True

match False:
case i.has_money:
log('no_money')
case i.has_time:
log('no time')
case i.has_friends:
log('no friends')
case i.has_internet:
log('no internet')
case _:
log('Yay!')

pi_xi
Автор

Sometimes changing to negative condition makes code less readable.

closetothee
Автор

Why don't you use if, elif and else statements instead of using multiple ifs and return statements?

Also for this example it was much easier to use the and operator, i.e. If a and b and c and d

sagshah
Автор

Defining log

He: log = lambda *args: print(*args)
Me: log = print

Madhava_P
Автор

If you need to do things in each level then you have a candidate for splitting the method into multiple procedures. Consider the responsibility of the unit of code. You want to reduce it. Increases comprehension and the ability to unit test.

JesseSteinfort
Автор

Early return pattern is great, even better if you only import a module after all of them are True.

adam_fakes
Автор

So this is how learning to code in Short videos look like ?

ngkien
Автор

satirical content like this shows that developers have no sense of humour

jolynele
Автор

Idk python, but in C++, you can make bool and string arrays and just run through them— this way, you don’t have to keep writing it out

brthhule
Автор

i'd prefer using if-else with no returns. if you had many such checks, you could build a dict and some pythonic logic for some clean code.

ytlongbeach