Тёмный
No video :(

Add THIS To Your Python Scripts! if __name__ == "__main__" 

b001
Подписаться 261 тыс.
Просмотров 171 тыс.
50% 1

⭐ Join my Patreon: / b001io
💬 Discord: / discord
🐦 Follow me on Twitter: / b001io
🔗 More links: linktr.ee/b001io
Background music:
Rain, Book And Cup Of Tea by | e s c p | escp-music.ban...
Music promoted by www.free-stock...
Attribution 4.0 International (CC BY 4.0)
creativecommon...

Опубликовано:

 

6 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 145   
@MattCantSpeakIt
@MattCantSpeakIt Год назад
For people who are confused as to why you'd want to import a script that executes code... You'd usually use this when making a library. You can run your library on it's own to include tests, or just display a help page or something like that when called on its own, which is code you don't want to be executed when you import the library for it's normal use.
@kimfom
@kimfom Год назад
so can you have several `if __name__ == __main__` clauses that calls different functions in one script?
@MattCantSpeakIt
@MattCantSpeakIt Год назад
@@kimfom yes, but there is no point in doing so. You'd usually have all of your imports at the top, then all the stuff thats always executed, then you'd have your "if main", in which you'd call anything and everything that you want to be called only if used on it's own. But lets say you have some initialization stuff in the script, and in the middle of initializing, you want to set something differently if the script is called on it's own, then the rest of the init... and then maybe another block of code to be called only as main. // imports // init phase 1 ... if _name_ == '__main__': // change something.. // init phase 2 if _name_ == '__main__': // do something (print?) Thats a bit spaghetti, but yes, that would work.
@kimfom
@kimfom Год назад
@@MattCantSpeakIt Awesome! Thank you for your response
@OmegaQuark
@OmegaQuark 8 месяцев назад
"it's own" -> its, *its*, ITS, without apostrophe, please. "its" is an adjective, a possessive determiner; "it's" is a shorthand for "it is" or "it has", the third person singular of the verb " to be" or "to have", respectively. It is (it's) really insane the amount of people on the web that write "its" instead of "it's" and vice versa. I suppose it is (it's) a problem that arises from automatic correctors, but it really takes no time to check. Even LLMs now sometimes write the one instead of the other due to the sheer amount of bad examples disseminated through all the web of people that make this mistake. It is (it's) bollocks. I am (I'm) sorry if I point out this fact under you comment. I do not know you, nor did I have the intension to do so. But enough is enough, I have (I've) seen too many wrong examples, and I cannot (can't) take it anymore; I had to comment to free myself of this burden. Please do not take it as a personal attack. Have a good day :)
@roxxel8167
@roxxel8167 8 месяцев назад
@@OmegaQuark that is the reason you never get invited to parties bro
@Mr.Not_Sure
@Mr.Not_Sure Год назад
This is actually hint to a reader of your code: If your script has *`if **___name___** == '__main__'`* clause, it means: "this script can be imported OR run standalone". If your script doesn't have the clause, then two cases are possible: (a) if it has imperative operations inside, it's standalone-only, top-level script; (b) if there are only functions/classes definitions inside, it's module-only. P.S. And yes, if your script doesn't have the clause AND also mixes imperatives with definitions, it's code smell, IMO.
@stevebot
@stevebot Год назад
Clevon approves of this.
@user-zu1ix3yq2w
@user-zu1ix3yq2w Год назад
Thanks for the explanation. It helps. Interesting.
@ghhdgjjfjjggj
@ghhdgjjfjjggj 2 месяца назад
what is standalone, what is script, what is clause, what is imperative operation, what is top-level script, what is defintion, what is module-only, what
@ghhdgjjfjjggj
@ghhdgjjfjjggj 2 месяца назад
what is standalone, what is imperative operations, what is top-level script, what is module-only
@Mr.Not_Sure
@Mr.Not_Sure Месяц назад
@@ghhdgjjfjjggj yes
@SiofraTural
@SiofraTural Год назад
I love how simple yet straightforward you are with these clips. I actually prefer the video format as opposed to shorts because if I miss a word or can’t understand something right away, I can quickly rewind. The curse of adhd lol. Keep up the awesome work on these Python videos. They’re a lot of help!
@xxmausermanxx8389
@xxmausermanxx8389 Год назад
Nice. Been learning python now for about 10 weeks. I started seeing this a bunch but with no real reason as to why. Makes so much more sense now
@nearyou30
@nearyou30 Месяц назад
Possibly the most concise and clear explanation of this variable!
@lilyempire
@lilyempire 22 дня назад
Finally. A very simple yet straightforward explanation. Thank you
@ivanichianus
@ivanichianus Год назад
watched a few other videos of this topic and yours was the most simple to understand, thank you! I wish you had a full Python course video, would love to watch you guide us through from the very basic beginnings to more complex stuff in Python.
@PracticalCoding.
@PracticalCoding. Год назад
Quick, simple and practical. Nice video!
@humzaashraf321
@humzaashraf321 Год назад
This is the simplest and clearest explanation I've come across for this. Thank you!
@erickthamara5154
@erickthamara5154 5 дней назад
Short and sweet,thanks
@heeroyuy298
@heeroyuy298 Год назад
Thank you this was helpful. Please make a video about the python import system. I've been a python dev for 5 years and still have no idea how it really works.
@rungsunchoongwattana8370
@rungsunchoongwattana8370 Год назад
I found this to be the best explanation of the use of __name__. Your illustration is simple and crystal clear. Many thanks.
@svampebob007
@svampebob007 Год назад
I found it more infuriating as to why you would execute a code on import? I'm a hardware kind of guy. If I wire chip 1 to chip 2 and ask chip 3 to execute what ever chip 2 can do, it will not start by saying: Ah I see chip 2 is connected to chip 1 so we better do what ever chip 1 is doing (unlocks device), chip 2 what do you do? security? 😳, what is my job? INTERFACE?!🤡 I'm pretty comfortable with basic, but anything above that I always get irritated: move A to B "well the developer expected you to do this before that... so you're expectations aren't real so we moved B to Z temporarly moved A to D, Y asked for C so K got B, now A and K are near each other so C can look at Z by looking at L, N always hold B so if you moved N to the left it would overwrite A".... because IF A = B was supposed to be written as IF [[ A = B ]]
@s.hamedstriker5315
@s.hamedstriker5315 20 дней назад
finally! an explanation to the use not complicated mechanism!
@dimdongo
@dimdongo Год назад
This is the best explanation I've seen yet. Good job!
@rodrigoguimaraes3690
@rodrigoguimaraes3690 Год назад
Now I've undestood why we should use this. Thank you
@hugoloopik2154
@hugoloopik2154 Год назад
Very good explanation, with good examples that do not only explain what it does and why you should do it, but it also how it works.
@Vitotheking100
@Vitotheking100 Год назад
Most useful video ever. I have seen so many code with this name==main and didn't get the point of doing it as the code runs perfectly fine without it
@brunoroda3437
@brunoroda3437 8 месяцев назад
When you learning, sometimes you just need little videos to have a whole new understanding of what you're doing :D thank you so much
@PutXi_Whipped
@PutXi_Whipped Год назад
Very clear explanation. Thank you very much
@plashplash-fg6hd
@plashplash-fg6hd Год назад
Thanks. For the longest time, I saw this command on GitHub and never knew what it meant. Now I do.
@rishikaushik8307
@rishikaushik8307 Год назад
for more clearity you could have printed __name__ in script1 and and imported it in script2 to see what it prints
@andrewaldrich3602
@andrewaldrich3602 Год назад
I saw this in No Starch Press’s python crash course book in the unit tests chapter. I wish they had gone more in depth with it
@somnathmanna5285
@somnathmanna5285 6 месяцев назад
Great tutorial... So many junks on RU-vid for this but you explain it very simply.
@bulelanibotman
@bulelanibotman Год назад
woah! what a clear explanation, thank you so much!
@waghdipak2358
@waghdipak2358 Год назад
really a very useful video, I got the clarity for the first time about this. subscribing your channel
@anj_____m
@anj_____m 10 месяцев назад
this is the third video on __name__ that i watched and it was the icing on the cake. i now fully understand what the purpose is, thank you so much!!!!!!!!!!!!!!!!!!
@Caenem_
@Caenem_ Год назад
So basically it's to avoid calling functions from an imported module with the line "from xxx import yyy", because we don't want to call them with this lane but later in the main program
@thecarlostheory
@thecarlostheory Год назад
I was wondering a time ago. Thx u so much for solving my doubt
@AstronomiyiKodlamak
@AstronomiyiKodlamak Год назад
ty for ur video, i couldnt understand why everyone are using this "nonsense" until now
@niksonney4462
@niksonney4462 Год назад
hello, what font and theme are u using ? btw, nice vid, gj
@philipberthiaume2314
@philipberthiaume2314 Год назад
Or you can remove the method call in Script1, import in Script2 and call the do_something() function from there.
@aufkeinsten7883
@aufkeinsten7883 Год назад
Don't have any real experience with python, is there a reason why code from a script runs by default when importing it? Seems weird to me
@rohankokatanur6426
@rohankokatanur6426 Год назад
I see , u have been learning python at a great speed.
@AMMIN13
@AMMIN13 8 месяцев назад
that is super simple. Really amazing!
@eljocacheur
@eljocacheur Год назад
Thank you man!
@imdonkeykonga
@imdonkeykonga Год назад
nice and clear example, failed to put in a context of entrypoint for the execution but thats not a big issue nice job
@all_things_narix
@all_things_narix Год назад
This is awesome.. I always wondered what it meant but i was too lazy to look it up. Thanks You
@oeufleau8543
@oeufleau8543 Год назад
Finally a good explanation
@user-ny1rx5ey6w
@user-ny1rx5ey6w 9 месяцев назад
Simple and useful explanation. Thanks. :)
@DaSnipy
@DaSnipy Год назад
super clear explanation .. thanks man !
@superben754
@superben754 6 месяцев назад
amazing..! Thank you for the video!
@EmailacS
@EmailacS Год назад
Very helpful,nicely demonstrated ty.
@awaisanjum9023
@awaisanjum9023 Год назад
Oh boy, amazing explanation
@elshanmammadli7731
@elshanmammadli7731 11 месяцев назад
Thanks a lot. Got it now.
@wh1pper457
@wh1pper457 10 месяцев назад
this was the explanation i needed. thank you
@jovaraszigmantas
@jovaraszigmantas Год назад
Thank you so much for the video
@aryangupta7703
@aryangupta7703 11 месяцев назад
Thanks, Awesome crystal clear explanation
@CrjaseMechaEngr
@CrjaseMechaEngr Год назад
Beutifully explained 👏
@funnyweirdovideos
@funnyweirdovideos Год назад
Wow! Thank you so much.
@NikhilPatel-ew6eb
@NikhilPatel-ew6eb Год назад
Which VS code theme?
@harrydunlop7841
@harrydunlop7841 Год назад
synthwave 87' im pretty sure
@thepranjalrai
@thepranjalrai Год назад
@@harrydunlop7841 it's '84
@pixiedev
@pixiedev Год назад
try mili theme because It awesome. And made by me 🙂.
@trimuloinsano
@trimuloinsano Год назад
But with neon off
@JacobTing-ow9sw
@JacobTing-ow9sw Год назад
It`s very useful and simple knowhow, thank you !
@christiansanchez9880
@christiansanchez9880 Год назад
Wow, this video made me finally understand this.
@djangoworldwide7925
@djangoworldwide7925 8 месяцев назад
Why wouldn't you remove the call to do something in script1 instead? Well I guess there are other real life complex situations where this is useful. Thanks it was straightforward
@3DimeMedia
@3DimeMedia Год назад
Very helpful, thank you.
@gabrielm7746
@gabrielm7746 Год назад
Thanks for the video.
@rosmisha
@rosmisha Год назад
Thanks for the cool video, you helped me
@CornFlaekk
@CornFlaekk 6 месяцев назад
What theme do you use for VSCode? It looks so good
@ajax_og
@ajax_og 6 месяцев назад
I wish i saw this video LAST NIGHT when I was doing my homework and could not for the life of me figure out how to not get my code to run twice... well you live and you learn lmao
@nomadontherun_
@nomadontherun_ Год назад
Finally! A clear and simple explanation of this....
@mohamedantar6865
@mohamedantar6865 Год назад
Wow very useful thank you
@AliAbbas-vp4bm
@AliAbbas-vp4bm Год назад
Coming from Java and just started learning python and this was one of the things that I didn’t quite understand
@ksspqf6016
@ksspqf6016 Год назад
Perfect explanation
@theblackelephant
@theblackelephant Год назад
Sir great content keep it up
@timraiser4811
@timraiser4811 Год назад
Please help me explain how it doesn't run twice after the if statement is added. He calls the function in script 2 without the conditional so why doesn't it run?
@JhamYT
@JhamYT Год назад
I'm confused about this too
@thorbjrngrnborg5113
@thorbjrngrnborg5113 Год назад
Because he is running it from script2, which isn't the "main" (the condition is not met) and therefore it's only being ran from script2 ie. once
@Kirmo13
@Kirmo13 Год назад
The explanation of what __name__ is is somewhat vague, but it's 1000% better of the explanation of why you would want to use it
@ramnivas5252
@ramnivas5252 8 месяцев назад
Bro you are awesome
@maksiksq
@maksiksq Год назад
Was really wandering what that thing in my StackOverflow'd code does. Thanks.
@zero_day_xsploit
@zero_day_xsploit Год назад
all the 30 min videos and this was the best
@Edd211
@Edd211 8 месяцев назад
# File: example.py def some_function(): print("This function is defined in the module.") if __name__ == "__main__": # This block will only execute if the script is run as the main program print("This script is being run directly.") some_function() else: # This block will execute if the script is imported as a module print("This script is being imported as a module.")
@AdvForTimeFrever
@AdvForTimeFrever 9 месяцев назад
i have a dumb question. why dont just remove " do_something() "
@calcio437
@calcio437 Год назад
More semplier than this is impossible to teach and explain. Good video
@marcuswest8085
@marcuswest8085 Год назад
Well_paced explanations...
@Hephasto
@Hephasto Год назад
So basically we add this clause to avoid duplicate call, right? Makes sense 🎉
@mamadoubah3143
@mamadoubah3143 8 месяцев назад
god, i have finally understood it
@ldavader2704
@ldavader2704 Год назад
What has confused me is that dot in your function name...is that a good practice?
@callyral
@callyral 8 месяцев назад
ikd python but wouldn't it be easier to add a guard clause before the "main execution" testing if it's NOT "__main__" and exiting if that's the case? so that your code isn't all indented
@avaneeshc
@avaneeshc Год назад
What is your vs code theme
@thepranjalrai
@thepranjalrai Год назад
Looks like Synthwave'84, which is what I use.
@userpmzjah82
@userpmzjah82 Год назад
so when you import with the if statement, it doesn't run when importing because __name__ = "script1", but it runs when you call it directly bc __name__ = "main" , right? Does java has the same problem of running twice?
@gopal.
@gopal. 5 месяцев назад
gawd I love you
@newsgo1876
@newsgo1876 Год назад
concise!
@soggy9648
@soggy9648 Год назад
or you could define a main method and call it afterwards. looks nicer and does the same thing
@Looki2000
@Looki2000 Год назад
Why can't you just remove do_something() line from the script1?
@mahmudhasan3093
@mahmudhasan3093 Год назад
Thank you
@rryann088
@rryann088 11 дней назад
C L E A N E S T 🔥🔥🔥🔥🔥🔥🔥🔥🔥
@theblackelephant
@theblackelephant Год назад
Can you create a full 10hours Django course. please
@s.t.6627
@s.t.6627 Год назад
Nothing like sex music and writing Python script at the same time
@saimqazi1663
@saimqazi1663 Год назад
Bro Your the best
@vishweshthotakuri6144
@vishweshthotakuri6144 Год назад
Hey do you have any course on udemy for python
@scrambledmc3772
@scrambledmc3772 10 месяцев назад
I don't get it. You're calling the function in both files but you only want it to run in one? What's the point of that? When would you ever need this? Point is, any code you want to run in only one file should only be written in that file. This can be applied similarly to variables knowing their own name.
@DMZT_dhruv
@DMZT_dhruv 3 месяца назад
Which Font are you using?
@ekanshjaiswal9976
@ekanshjaiswal9976 6 месяцев назад
Interesting . . . .
@vladthecoldone8804
@vladthecoldone8804 10 месяцев назад
or you could simply not call the function in the module from which you import? can someone please explain to me why is it needed? I guess I know what it does, syntax and whatnot but simply put, if youre not calling the functions/methods in not main module, why would you even use it when it doesnt run automatically? Edit: okay I see it, useful only when you need to use it as a standalone script for testing so you can have bunch of function calls ready at hand otherwise no need if you aint calling it and why would you even call it or make print statements there since thats whats main module for
@PopsFizzle-wz5jd
@PopsFizzle-wz5jd Год назад
I need the name of that theme please
@iurymikaelsobraldossantos646
@iurymikaelsobraldossantos646 6 месяцев назад
What theme are you using on vscode?
@bidhanry9740
@bidhanry9740 Месяц назад
Where is the Fire react button on youtube? Only Like would not help in this case, I need a fire react button.
@yxngboypolo
@yxngboypolo Год назад
WHAT THEME ARE U USING
@AdityaSharma-wg4rj
@AdityaSharma-wg4rj Год назад
Waw... Beautiful......
@wesdavis8797
@wesdavis8797 Год назад
Great stuff. Treehouse's lesson about this (which their lessons are usually great) was awful.
@mccauleybacalla2228
@mccauleybacalla2228 7 месяцев назад
As a beginner, i am confused fr
@dadecky5276
@dadecky5276 Год назад
helpful
Далее
If __name__ == '__main__' for Python Beginners
9:29
Просмотров 19 тыс.
*Args and **Kwargs in Python
3:49
Просмотров 272 тыс.
Why You Shouldn't Nest Your Code
8:30
Просмотров 2,7 млн
How To Use Recursion In Python
8:50
Просмотров 7 тыс.
Python Tutorial: if __name__ == '__main__'
8:43
Просмотров 2 млн
Automate your job with Python
6:07
Просмотров 401 тыс.
The Algorithm Behind Spell Checkers
13:02
Просмотров 412 тыс.
THIS is Why List Comprehension is SO Efficient!
5:25
Просмотров 171 тыс.
Naming Things in Code
7:25
Просмотров 2,1 млн