Book Image

Secret Recipes of the Python Ninja

Book Image

Secret Recipes of the Python Ninja

Overview of this book

This book covers the unexplored secrets of Python, delve into its depths, and uncover its mysteries. You’ll unearth secrets related to the implementation of the standard library, by looking at how modules actually work. You’ll understand the implementation of collections, decimals, and fraction modules. If you haven’t used decorators, coroutines, and generator functions much before, as you make your way through the recipes, you’ll learn what you’ve been missing out on. We’ll cover internal special methods in detail, so you understand what they are and how they can be used to improve the engineering decisions you make. Next, you’ll explore the CPython interpreter, which is a treasure trove of secret hacks that not many programmers are aware of. We’ll take you through the depths of the PyPy project, where you’ll come across several exciting ways that you can improve speed and concurrency. Finally, we’ll take time to explore the PEPs of the latest versions to discover some interesting hacks.
Table of Contents (17 chapters)
Title Page
Copyright and Credits
Packt Upsell
Foreword
Contributors
Preface
Index

Launching Python environments


By default, Python is installed on a computer with the Python interpreter included on the system path. This means that the interpreter will monitor the Command Prompt for any call to python

The most common usage for Python is to run a script. However, it may be desirable to launch a specific version of Python for a specific program.

How to do it...

  1. The most basic command to execute a Python program is as follows:
      $ python <script_name>.py
  1. The following examples show how to launch specific versions of Python, as needed:
      $ python2 some_script.py # Use the latest version of Python 2
      $ python2.7 ... # Specifically use Python 2.7
      $ python3 ... # Use the latest version of Python 3
      $ python3.5.2 ... # Specifically use Python 3.5.2

How it works...

Calling python2 or python3 opens the latest installed version of the respective branch, whereas the other examples show how to invoke a specific version number. Regardless of whether a newer version...