Python Shorts — __name__

John Clark Craig
3 min readSep 8, 2022

Python makes it very easy to organize and structure our program parts for efficient and understandable recycling and reuse.

Photo by Roman Synkevych 🇺🇦 on Unsplash

Copy and Paste

It’s easy enough to copy and paste lines of code such as functions and class definitions from one Python program to another. But there’s a much better way.

Collect related functions in a single Python file, add some code at the end to provide working examples of their use, and then import that file into any program where you want access to those functions.

About Those Working Examples

That’s all fine, but how do you keep the working examples code at the end of a Python module from interfering with programs that import such a file? It’s actually very easy, and there’s a good chance you’ve already seen the somewhat mysterious looking line of code that does the trick. Let’s take a closer look.

plane.py

Edit the following code into a file named plane.py to create a tiny “library” of plane geometry functions. This one function returns the distance of any x,y point from the origin, but it would be easy to add a whole bunch of other plane geometry functions to this file. For now, let’s keep it very simple:

# plane.pydef distance(x, y)…

--

--

John Clark Craig

Author, inventor, entrepreneur — passionate about alternate energy, technology, and how Python programming can help you hack your life.