If __name__ == '__main__' for Python Beginners

  Рет қаралды 21,147

Indently

Indently

Күн бұрын

Пікірлер: 38
@hunterap2355
@hunterap2355 Жыл бұрын
The bigger use for if-name-is-main check is that you need it when using any library for multithreading / multiprocessing (threading / multiprocessing / asyncio / concurrent.futures). The way it works is that when creating a new thread/process, you point to a function defined in your code for the thread/process to work on, and if you don't have the if-name-is-main check then you would end up creating a fork-bomb, where each new thread/process would think it should run the main program and create new threads/processes infinitely.
@smitbangare9526
@smitbangare9526 Ай бұрын
Thanks man for info :)
@nickjames2651
@nickjames2651 10 ай бұрын
Dude thank you so freaking much!!! IDK why this was so hard for others to explain in a simple video haha literally so many other videos have the music blaring, are reading from a script and just superfluously repeating things. This video made so much sense thank you again!!!!
@utarasama
@utarasama 9 ай бұрын
Yeah, I think the worst thing to do in a tutorial is to put any kind of music in the background. I can't focus like that
@Tara-zv1jj
@Tara-zv1jj 7 ай бұрын
Been stuck on this in a class for DAYS and just the first three minutes of thisvideo immediately cleared up so much that it was shocking. Thank you!
@818de60
@818de60 Жыл бұрын
im new to python, and i was wondering why people use that. thanks for the tip
@komaljoshi9347
@komaljoshi9347 5 ай бұрын
this video makes so much sense.. better understanding now.. thanks :)
@WojciechowskaAnna
@WojciechowskaAnna Жыл бұрын
normally in the module, you have function definitions. What you do in your module, you added a function call (subscribe() ). That's why when importing is being executed. It's for me strange practise (to add fucntion calls outside of functions inside your modules), but the rest is ok.
@denisdzhindo2422
@denisdzhindo2422 Ай бұрын
Thanks! Very clear!
@DrDeuteron
@DrDeuteron Жыл бұрын
The most import reason to include name/main is to tell people your module is also an executable script, and if it is only a script, you can now use OO and functions to deal with your problem in a clear and SOLID manner. No more incomprehensible bash or, gasp, perl, scripts.
@aafan.kuware
@aafan.kuware Жыл бұрын
in my case if there are multiple functions, do I need to add if main in both the functions? def abc: ... if __name__ == 'main' : abc() def efg: ... if __name__ == 'main' : efg()
@castlecodersltd
@castlecodersltd Жыл бұрын
As usual a clear and concise tutorial. Thank you ☺
@Nabilh17
@Nabilh17 Жыл бұрын
Very clear, thank you
@hailehaile8229
@hailehaile8229 7 ай бұрын
I have a question. Does it affect the script's performance by adding it?
@skyisthelimpet
@skyisthelimpet Жыл бұрын
Quality content as always
@leanghok120
@leanghok120 Жыл бұрын
Can you make a video about setting up pycharm with the new UI for python dev like main setting and stuff
@Indently
@Indently Жыл бұрын
I will make more videos about PyCharm soon. I made a video on NewUI, it's literally 1 button to set it up.
@techie18
@techie18 Жыл бұрын
That's great 👍 👌
@olaniyanayodele5986
@olaniyanayodele5986 Жыл бұрын
Do a video on your pycharm setup please
@Indently
@Indently Жыл бұрын
I will try :)
@Fireman9143
@Fireman9143 6 ай бұрын
How did you setup to auto-populate that!?
@ytusr-ko5rg
@ytusr-ko5rg Жыл бұрын
Nice video:D
@krystian6364
@krystian6364 Жыл бұрын
So, if you're saying that importing only a specific function like "from sample_module import subscribe" doesn't really save resources because python checks everything in a given module (in this case sample_module), then why not use "import *" every time? Is it just that when I import several libraries in this way, their functions may have the same names, which can lead to errors later? Thanks
@shaurryabaheti
@shaurryabaheti 8 ай бұрын
actually, libraries have global variables that you won't know about unless you view their code and if by any accident you override said variables it can lead to the library not being useable (in that instance) also modules are huugee and flooding your namespace with so much is not good, import either the full module, eg, import tkinter as tk or import parts of it eg, from tkinter import Tk never import everything, import * is very respurse heavy as it takes up a lot of your memory.
@wirrexx
@wirrexx 8 ай бұрын
Correct me if I’m wrong. But if __name__ == __main__ should only be written if the part you are working on is a script. Not on a module ? Main == main app.
@cy_xyz
@cy_xyz Жыл бұрын
God video, som altid!
@20becky35
@20becky35 7 ай бұрын
if __name__ != 'name' : the code underneath it won't be excuted in the main module but outside the main module it is possible to use any element embraced by the if statement. am i right guys? i am not familiar with Python
@scullyy
@scullyy Жыл бұрын
The live template functionality is wack for me. It only works if I write "if main", not "main". And then of course I have to remove the "if" part of the code otherwise I get double ifs: "if if __name__ == "__main__:"
@kiwiwelch3620
@kiwiwelch3620 Жыл бұрын
5 years in and still to this day I wonder about name == main
@rishiraj2548
@rishiraj2548 Жыл бұрын
Good day greetings
@TheosTechTime
@TheosTechTime 11 ай бұрын
If this script is a program then run program code
@carlabest1257
@carlabest1257 Жыл бұрын
I enjoy your videos, but.... This particular video on the use of __main__ is for me, not very useful, and I never use this constuction. Let me explain. I use an "interactive environment". I never run scripts from the command line. In my REPL environment, any script can be the "main" script at any time. Therefore, using the __name__ = __main__ statement can never have any effect. However, I do have debugging techniques that comment and uncomment statements. That is more useful. Also, I generally debug one function at a time. When I do, I transfer the code in the function to its own module, and execute the statements one a time. This is far more effective than having a check for __main__ in every module. But still, maybe for some people who do execute scripts from the command line, this technique might actually be beneficial. I can't imagine using any computer language from the command line. An interactive environment, and if possible a visual environment is an absolute must. If a new language can't provide that it can't be of much use. Also, showing the data type for every variable (example - x:int = 85) or for arguments in a function is only marginally useful. I use Python because it allows me to get things done. I appreciate Python's flexibility in making me - the programmer - productive. My functions rarely take a single data type as an argument, and the same variable is often assigned different data types. Using data type descriptors is sometimes useful, but often, if used, would be inaccurate. If Python were to force the use of data type descriptors it would restrict the language to an unnecessary degree. So, I appreciate the use of some constructs in Python when they make sense, but I hope they never become more "pythonic" by making them mandatory.
@OneMoreCasst
@OneMoreCasst Ай бұрын
I don't know who is copying who but there is the 3 videos of the same thing lolol. Not even kidding. All 3 of the videos also have lots of views lol. Ppl acting like they can code but doing it for youtube views.
@kiwiwelch3620
@kiwiwelch3620 Жыл бұрын
pycharm is too much noise
@imfrommars7362
@imfrommars7362 Жыл бұрын
nobody's asking you to use it
@arokiarajan1230
@arokiarajan1230 19 күн бұрын
Not very clear ... sorry
@Indently
@Indently 19 күн бұрын
Don’t worry, if you continue studying you’ll understand eventually! :)
@piquanttorture
@piquanttorture 8 ай бұрын
Hello But only if you love Python.
The FULL Guide To Itertools For Python Developers
29:11
Indently
Рет қаралды 16 М.
5 Wacky Python Features
15:37
Indently
Рет қаралды 9 М.
When Cucumbers Meet PVC Pipe The Results Are Wild! 🤭
00:44
Crafty Buddy
Рет қаралды 55 МЛН
БУ, ИСПУГАЛСЯ?? #shorts
00:22
Паша Осадчий
Рет қаралды 2,8 МЛН
The Singing Challenge #joker #Harriet Quinn
00:35
佐助与鸣人
Рет қаралды 42 МЛН
5 Good Python Habits
17:35
Indently
Рет қаралды 618 М.
What does "if __name__ == '__main__' " do in Python?
14:03
Python and Pandas with Reuven Lerner
Рет қаралды 5 М.
5 Useful Python Decorators (ft. Carberra)
14:34
Indently
Рет қаралды 106 М.
If __name__ == "__main__" for Python Developers
8:47
Python Simplified
Рет қаралды 414 М.
How To Use: "@dataclass" In Python (Tutorial 2023)
15:01
Indently
Рет қаралды 29 М.
How To Write Better Functions In Python
14:17
Indently
Рет қаралды 48 М.
10 Important Python Concepts In 20 Minutes
18:49
Indently
Рет қаралды 296 М.
The Complete Guide to Python Virtual Environments!
15:52
teclado
Рет қаралды 366 М.
When Cucumbers Meet PVC Pipe The Results Are Wild! 🤭
00:44
Crafty Buddy
Рет қаралды 55 МЛН