Best Practices for Python Main Functions

preview_player
Показать описание
You’ll learn about four best practices you can use to make sure that your code can serve a dual purpose:

1. Put most code into a function or class.
2. Use __name__ to control execution of your code.
3. Create a function called main() to contain the code you want to run.
4. Call other functions from main().

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

Best explanation I've ever seen on YouTube about Python.
It's impossible to explain it more clear than that.
Congrats!

UlissesOliva
Автор

I've bought quite a few e-courses - not in vain I've learned a lot - but none of them has come even close to explain main like you; rather confused me! As another one wrote, this is pure gold. Now I not only understand the main function I even see how, and why, it will be quite an implement - a great tool - in my current ongoing projects. Thank you so much!

jakobfredriksson
Автор

Masterfully explained. I loved that you reviewed the learning objectives at the end, but with a more in-depth explanation. Fantastic, easy to understand. Thanks.

bradydyson
Автор

That was an excellent explanation! I've always wondered whether to put my main code in the conditional or actually use a separate "main" function. Thanks for explaining it!

johnjsal
Автор

That was the best explanation for __name__ I have ever heard.

JohnBoen
Автор

dude, these are practices that aren't really spoken about, new programmers are just expected just to do them without any real explanation. Awesome content man

azr
Автор

Very well constructed and delivered! Bravo!

musicarroll
Автор

Best explanation for this I've seen for a long time

kenmurphy
Автор

Very nice! :-) For beginners, this should be a video to watch as mandatory. Can you extend this and have a best practices, if you want to have a bigger project of, for example, 2-3 files? One with functions, one main. And how to integrate "logging" into them or loading "config" from a xxx.conf file, for example with ConfigParser or ConfigObj?

songokussjcz
Автор

Thank you, you helped me a lot. It was simple, straightforward, concise.

TheSophiaLight
Автор

Your explanation was very useful for me. Thank u very much!!

manuelmarinduque
Автор

wow the quality of this video is really good. but i have to watch this video a couple of times to fully understand everything.

satoshinakamoto
Автор

There seem to be some variants of this, what is the difference between the following? if __name__ == '__main__': sys.exit(main()); if __name__ == '__main__': exit(main()); if __name__ == '__main__': main()

MJF
Автор

great leasson... pretty balance between deep and short material :) I suggest videos on good practices in different software architectures, as well as an analysis of pros and cons on which to choose given a real case ...

riptorforever
Автор

This is exactly what I was looking for. Thank you !

investandcyclecheap
Автор

Have you done this a different way ?
def number_of_countries():

def main():
print(number_of_countries)
if__name__== “__main__
main()

This is how the teacher wants our lab setup for tuples

JgORaYa
Автор

If you have no specific plan to use a part of the functionality by importing the file as a module, is this practice really useful? The length of the script inflated a lot. If you know that you're going to use the functionality, why don't you separate the reusable part into a module and write a small script main.py which imports the module and calls the functions?

noriotakemoto
Автор

You miss one good practice point...
You may have to name your main file "__main__. py" to be able to have a package to be run from named parent directory and that way no confusion about the first main file to run.

DGDG
Автор

You did not show the example of the first rule: put into the class

hifromspace
Автор

I have the following questions (FYI: I run pycharm with Python 3 on Windows 10):
the 'python3' command did not work in the pycharm terminal. Instead I had to use 'py'

when I use the 'import' statement, I get the following error:
'import' is not recognized as an internal or external command,
operable program or batch file.

when I add and run:
if __name__ == "__main__":
main()

I get:
Traceback (most recent call last):
File "C:\Users\*\PycharmProjects\Exercises\best_practices.py", line 20, in <module>
main()
NameError: name 'main' is not defined

Could someone explain to me why all these functions do not work as in the video?

Thanks in advance

bdev