Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Double Unders1:20 with Ken Alger
Python has some special methods available to developers that utilize the double underscore character plus the method name.
Hi, I'm Ken!
In this workshop,
we'll talk about using a recommended Python best practice, dunder main.
In Python, there are naming conventions for some special methods
that utilize the double underscore character, along with the method name.
As it gets a little tiresome to say __method name__, the Python community
has adopted the name of dunder for double under to describe these methods.
I won't be covering all aspects of these dunder methods in this short workshop,
but I do want to cover a specific one, dunder name.
When you execute a script using the Python executable
followed by the name of the script, it runs all of the code in the file.
This is usually what you want, but as you know, Python files can also be imported.
When that import happens, it also runs all the code in the file.
When a script is run, Python sets the dunder name to equal underscore,
underscore main or dunder main for the top level code being executed.
We can utilize this to our advantage to check if our script is being
run versus being imported, and handling those conditions as needed.
Lets head into Workspaces to see an example and
why it's a best practice to include in our code.
You need to sign up for Treehouse in order to download course files.Sign up