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