You should put this in all your Python scripts | if __name__ == '__main__': ...

preview_player
Показать описание
Python's favorite unexplained incantation!

Do you know def main if __name__ == '__main__'? In this video I explain why your Python scripts should use this idiom. It's not strictly necessary, but you will be a better Python coder if you follow established conventions that improve the readability of your code.

SUPPORT ME ⭐
---------------------------------------------------

Top patrons and donors:
Jameson, John M, Laura M, Pieter G, Vahnekie, Sigmanificient

BE ACTIVE IN MY COMMUNITY 😄
---------------------------------------------------

CHAPTERS
---------------------------------------------------
0:00 Intro
0:58 Dunder name
1:47 Why if name main
3:36 Why def main
4:59 Pickle example
5:42 Multiprocessing example
6:42 Main function entry point
7:07 Outro
Рекомендации по теме
Комментарии
Автор

I've used python for a while now, and I had no clue about the unwritten rules. Thanks for this

stevenalexander
Автор

Another reason is that code inside functions actually runs faster (in CPython), since the used Opcodes are `STORE_FAST` and not `STORE_GLOBAL`. That is very noticeable in scripts that do a lot of loops for example.

megaing
Автор

Reminding me about global variables converted me

StephenRoseDuo
Автор

this stuff is great for someone who has learnt to code at uni, but you just get chucked in the deep end. see stuff like this everywhere but don't know what it means. great explanation

zacbailey
Автор

As someone just starting to learn Python, this gave me a headache :)

I have so much to learn...

rubenduarte
Автор

I am learning python and saw this in various places but didn't know what it did or why I should use it. Now I finally understand the reasoning behind it and why it should be used. Seems like this is something that should be explained by anyone who is going to teach python to others.

hossumquat
Автор

This flashing "captions" are VERY funny. Keep 'em coming!

aleksanderkrauze
Автор

You could have also mentioned "test driven programming": In test driven programming first some test code is written, which will check whether the main() function does what it is supposed to do. Having all important code in functions is needed to make it callable from those outside test modules. pytest is a great library which aids the test driven approach to programming.

pefu
Автор

Another reason to use a main function is it makes it easy to turn a script into a library. At one point, I made a script with a custom class. Then, in another script, I realized that class would be really useful. Since the first one had a main function, I could simply import that class from the first script.

Shynaku
Автор

I really appreciate your vids and that you go the extra mile of example and explanation. 99% of tutorials show a cursory overview of some new shiny library or module, scattering global variable spaghetti to the four corners of your code base. In these days of stackoverflow copy pasta, your vids remind us that frequently more elegant and reliable implementations are not always 200% more effort.

ianrickey
Автор

Thanks, I’ve been writing Python for quite a while and it never came across to me that leaving those variables global is a potential source of bugs. Now I’ll definitely put all those stuffs inside the main function!

susu
Автор

At first i was like this dude wants to flex with fancy looking code, then came global variables in a pickle file. Consider me converted 🙏

dwarakanandan
Автор

Another reason to do this is so you can order functions and functionality (except for the trivial and idiomatic if-name-main-main block) top-down, and thus roughly in the order they will call each other, which makes the code much easier to comprehend and understand.

zTJqsl
Автор

I do the same, but had a different motivation. I like it when I read a file top to bottom that the written order somewhat resembles the execution order. So a main function with high level function calls that describe the entire script will be the first thing you read.

jjbankert
Автор

im a very new programmer starting a CS degree, my first programming class, once we learned what functions were, this was the first thing he taught us. Ive used it ever since

esmithk
Автор

"C programmers know", yes that was like a personal attack

You-qeqb
Автор

Wow, I've been trying to figure that out for so long, and literally nobody I talked to could explain it. Thank you so much for finally clearing up the confusion

spacehooliganzack
Автор

Really cool tip. As someone who just uses python as a hobby language, little tips like these are super helpful. Thanks

pinkdiscomosh
Автор

Yes, there’s a reason why the “higher learning-curve languages” like Java or C++ have a bigger learning curve than Python. It’s not because the people who designed Java were too dumb to create a language with a small learning curve, but rather because the additional structure of these higher-learning-curve languages is actually useful. We can manually add the additional structure of these more advanced languages to Python to gain the structural advantages, but then Python loses the advantage of being a easy-learn beginner language.

mensaswede
Автор

I think I will never not feel horrible(?) whenever I use a dedicated `main` function, even if I understood all of the points you've given. Gotta get over this mindset

re.liable