What does if __name__ == '__main__' do in Python?
Skills:
Python for Data80%
Key Takeaways
The video explains the use of if __name__ == '__main__' in Python, a convention that allows control over when code is executed, specifically when a module is run directly versus being imported.
Full Transcript
I'm willing to bet that most of you have seen this code before and probably have no idea what it actually does so in this short video I'm going to break that down so let's go ahead and get started all right so let's get rid of this to begin because in order to understand its use case we need to have a look at some sample python code so look here I have this calculator. py file now this is a file that I might directly run myself and if I want to do that I can press run and you can see that it says this is a simple calculator I can enter some numbers and then I get the and the difference of those values very simple and something that you've probably written many times before this is great and it contains some functions now these functions are things that are reusable right and we may actually want to reuse them in some other python code so for example I may have another Python program let's call it program. piy and we may want to reuse some of the logic in our calculator program so rather than copying and pasting these functions over we simply import this python module which is called calculator and then we can utilize the functions like add do subtract inside of this python script but watch what happens when I run this python code going to go ahead and run it and you'll see that it starts by saying this is a simple calculator now that's not what that said in this script this script says using the calculator module that's kind of weird and it's asking me to enter a number but that's not inside of this script now let's just go through it and notice that after it gives us the result it then says using the calculator module and gives us all of this output in fact what happened here is we started by running all of the code that was inside of this calculator module and then we ran all of the code that was inside of this program module now the reason that happened is because whenever you import something in Python you run the entire python module you start by executing the entire thing and anything that's in the main line of the program so stuff like the print statements will be ran even if that's not what you intended to do so this is exactly where the if name underscore uncore is equal toore uncore maincore uncore comes in this is a convention that allows you to control when code is executed so I'm going to add it here and you're going to immediately see how it works okay so let's type this correctly and I will break down what these variables mean okay so you can see that I have if name is equal to main with the double underscores and by the way this is sometimes referred to as Dunder so Dunder name or Dunder main which means double uncore name double _ Main and when you do this what's going to happen is this code will now only execute if this module is ran directly not if it is imported so let me show you what I mean now if I go and I run my calculator. py file you can see that this works and it runs but if I run my program. py file it no longer runs this code it just runs whatever is directly inside of program now the reason for that is because of this variable right here called name now name is is going to be equal to Dunder name if you directly execute this file so if you run this python file itself if you do not directly run this python file let's have a look at what this is equal to by simply printing it out so let's go here and let's print uncore uncore name uncore uncore okay so we're going to start by running calculator python calculator. piy okay it's equal to main now we'll say python program. piy okay and and why is this not working oh that's because it's in the line here okay so Python program. piy and you can see that it's equal to calculator which is the name of the module so if something has been imported then the name variable within that script will be equal to whatever the name of the module is whereas if you run it directly then it will be equal toore main so that's literally it that's why you use this ifame convention because a lot of times you write code in a mod that you only want to execute if that module is ran directly you don't want it to execute if you're importing this code from another module so you use this convention and obviously if there's something you always want to run then you don't put it inside of this if statement this is a good habit to get into and definitely something you could be asked in a python interview so make sure you understand it so that is almost it but I do want to let you know that I have a completely free coding newsletter that you can sign up for from the link in the description I'll send you coding challenges solutions to those challenges cool project ideas andp spiring stories Etc and I'll even give you a free guide on how to make money from coding which breaks down 14 practical ways to do that again from the link below if you are interested in any private mentorship from me then I do have a program called Dev launch I have two spots available right now so if you'd like to apply for Dev launch you can go to the link in the description training. techwith tim. net fill in a quick form and we'll see if you're a good fit and if I can help you out someone from my team will reach out to you anyways I hope you guys enjoyed if you did make sure to leave a like subscribe and I will see you in the next one f [Music]
Original Description
Want to work directly with me to level up as developer,? Apply to DevLaunch here: https://training.techwithtim.net
I'm willing to bet that most of you have seen this code before, and probably have no idea what it actually does. In this short video, I'm going to break that down,
🚀 My Software Development Program: https://coursecareers.com/a/techwithtim?course=software-dev-fundamentals&campaign=youtubedescription
📬 Join my Newsletter: https://techwithtim.net/newsletter
🎓 Get private mentorship from me: https://training.techwithtim.net
⏳ Timestamps ⏳
00:00 | The Problem
01:57 | The Solution
04:19 | Free Guide
Hashtags
#Python #PythonFunctions #SoftwareDevelopment
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Tech With Tim · Tech With Tim · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
A* Path Finding Algorithm(Visualization)
Tech With Tim
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
Python Programming Tutorial #3 - Conditions
Tech With Tim
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
Python Programming Tutorial #6 - For Loops
Tech With Tim
Python Programming Tutorial #7 - While Loops
Tech With Tim
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
Python Programming Tutorial #10 - String Methods
Tech With Tim
How to Overclock a NVIDIA GPU
Tech With Tim
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
Python Programming Tutorial #12 - Functions
Tech With Tim
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
Cool VBS Script to Prank Your Friends!
Tech With Tim
How to Overclock an AMD GPU
Tech With Tim
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
Recursion and Memoization Tutorial Python
Tech With Tim
Ethereum Mining Rig - Hardware Guide
Tech With Tim
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
How to Install Pygame (Windows 8/10)
Tech With Tim
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
Linear Search Algorithm - Python Example and Code
Tech With Tim
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
Binary Search Algorithm - Python Example & Code
Tech With Tim
Pygame Tutorial #5 - Projectiles
Tech With Tim
Pygame Game - Mini Golf
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
Pygame Tutorial #6 - Enemies
Tech With Tim
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
How to Create A Message Box in Python - Tkinter
Tech With Tim
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim
More on: Python for Data
View skill →Related Reads
📰
📰
📰
📰
MiniMax Raises HK$16B, Goldman Sachs Urges Pivot to China AI Value Chain
Dev.to AI
seq 14. Great Powers Climb, Then Pull the Ladder Up
Medium · AI
Why U.S. Stocks Keep Rising Through an Energy Shock
Medium · AI
L’IA di Google fa aumentare il consumo di energia elettrica del 37%
Medium · Data Science
Chapters (3)
| The Problem
1:57
| The Solution
4:19
| Free Guide
🎓
Tutor Explanation
DeepCamp AI