Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialShmuel Zilberman
1,409 Pointsi dont get the point of this video
Michael Russell
Courses Plus Student 8,550 PointsHey Shmuel, I appreciate you asking questions, and I feel this could have been introduced better, but please work on the way you phrase your questions. They show up as the title of the question and make it hard for others searching the Community for answers to their questions.
anthony pizarro
1,983 Pointsits actually pretty simple, and any person with common sense can rationalize this question. (im talking about Behar which didn't have to be rude and smirky )
1st..ok so "dunder" stands for "double under" because of the double underscore used in name or main
2nd.. if you want to use a function from another file you would use "import and 'name of file' " he gives the example of "import app"
3rd but if you only use import, the whole file would be running (imagine having loads of codes running when all you wanted was one part of it )
4th by using name(this can be read as "the file name") ==(which just means equals to) main(which can be read as the main file name ) then run this code.
5th when you import the file, you can run the code from the imported file by calling it like so, FileName.function(), the example used in the video is app.print_hello()
6th using this is very helpful when using "if" statements and you want to run a code from another file if the "if" statement is true or the "while loop", lets say you want to separate data types in different files and want access to them only when the user asks for it or the user only wants to see parts of the data in the file.
im also new to python and if anything that I said could have been said better I would really like to hear it, but as Michael Russell pointed out, your question could have been more defined to help you out
6 Answers
Mirza Gogic
4,342 PointsThere is a really great explanation on hows and whys of the name == 'main' here: stackeroverflow.com
Basically, you (hopefully) won't always be programming a "simple", one-file scripts. Sometimes you will have .py files that can be used in other programs ( I guess you call these modules). Instead of just C/P the code from one file to another, you can import it (just like the: import app command, where app.py was imported to second_app.py.
In some cases, you would not like a piece of code to be executed autmotacally, unless you are running the file as a main file. Here, you could use conditional if name == 'main' to make sure that the piece of code won't run automatically, if the file is imported to another file that is being executed.
Enrica Fedeli-Jaques
6,773 Pointsthanks for the link to stack overflow! quite clear. (I still didn't understand 100% but much better :) )
Duc Bui
14,546 PointsIt has to do with using Flask to make an application website.
ranitank
1,181 PointsI do agree this topic can be thought better for sure....but i do think i got the main concept
amyyxt
2,227 PointsI agree with ranitank . Also, I think the answers to the two questions in the first of this pair of videos do good jobs of explaining why this pair of videos was made/what they're trying to teach us, especially together: https://teamtreehouse.com/community/understanding-double-unders-main & https://teamtreehouse.com/community/double-unders
Summary of the discussions for those questions: For clarity below, when I say "top-level" script, I mean the file being directly run, which is the one whose name Python automatically sets to "main". By extension, "upper-level" refers to any code from top-level down to the level that calls or imports a script/file of interest.
Python automatically runs imported code as it goes through your script. The dunder check (e.g. if name == "main":/n/s/s/s/smain()) allows your script to circumvent the otherwise automatic execution of the main/principal part of a file by putting the principal part in a function usually called main() and having main() run only when the script is part the top-level (aka main) file (or when the specified by an upper-level file).
The way most of our projects, challenges, and workspace files have been and continues to be for at least a few more videos (I haven't gotten too far ahead) is not to include the dunder check and also to have the chunk of code that usually would be part of a main/principal function in normal Python programming instead be simply part of the principal script of the file. This is fine for now as what we do is simple and mostly for learning or testing code. So, our code files have been following a structure/order that looks like this:
- import files (if any)
- define classes (if any)
- define functions (if any)
- principal script of this file
What happens when such a file is imported (rather than directly run) is something like this:
- (further) files (if any) are imported and run (side note for clarity: this is an imported script importing more files)
- classes (if any) are defined (but instances are not (yet) created)
- functions (if any) are defined (but calls are not (yet) made)
- principal script of THIS file is run That means that the principal script of this file (which is NOT the top-level script in this scenario) will run regardless of whether this script is the top-level script or an imported script.
A common order/structure for Python scripts in the real world is:
- import files (if any)
- define classes (if any)
- define functions (if any)
- define main/principal function (most of what we've been putting in the principal script of our files would go here)
- dunder check with some condition leading to the call for the main/principal function
When you import a script of this structure, "everything" in the imported script still runs. Of course, statements for UNsatisfied conditions don't run, which is standard and intuitive. Specifically, with the generic dunder check, when the script is imported, its name will not be "main", and the dunder check is NOT satisfied. As a result, the main/principal function will not run (unless specifically called by another function or script).
What that means is that:
- (further) files (if any) are imported and run
- classes (if any) are defined (but instances are not (yet) created)
- functions (if any) are defined (but calls are not (yet) made)
- main/principal function is defined (but it is not (yet) called)
- The statements for whatever condition is met will be run. For the generic dunder check condition, the condition for running the main/principal function of this script would NOT be met if the script is imported (rather than directly run), so the main/principal function of this script will NOT be called (unless specified in an upper-level function or script). On the other hand, if this script were the top-level script, the main/principal function would be called. Note that any unconditional code or code for satisfied conditions will run. Specifically, all further files (if any) still have been imported and run, and all classes and functions (if any) in the imported script still have been defined as they are outside of the unsatisfied condition/are not conditional. These importations and definitions allow upper-level functions and scripts to use the files, classes, and functions imported or defined in this file. Whether specific instances, automatic or not, are created or run is independent of their importations or definitions.
Ryan Hartigan
3,426 PointsThey should double the length of this course and explain it better.
Brian Davis
1,628 Pointswhy didn't they just figure out a way to incorporate "file name" into the syntax? I had no idea that that's what "name/main" was referring to until reading a bunch of people's responses.
behar
10,799 Pointsbehar
10,799 PointsThis is not a question. So I guess all I can say is, "Ok?".