Python language basics 23: using the global __name__ attribute
June 21, 2015 Leave a comment
Introduction
In the previous post we saw one way to break up the number guessing game into short functions. We saw examples of void methods and functions with and without parameters. We also said that there are usually several possible ways to organise the code into functions.
However, we also saw that calling the number guessing game from the command line didn’t execute anything. Python has no way of knowing what you want to do with the code, it only sees a collection of functions.
The solution is easy but we need to learn a new language construct first.
Double-underscore variables
Python has a small number of built-in variables surrounded by double underscores ‘__’. These are populated by Python during code execution, i.e. the Python runtime, and can be accessed in code if you need them.
One such variable is…