June 2019
Beginner to intermediate
770 pages
19h 24m
English
An alternative to an if statement is to use a try statement to locate a candidate's implementation. This technique works well when there are different distributions. Often, a platform-specific distribution may include files that are unique to the platform.
In Chapter 16, The Logging and Warning Modules, we showed you this design pattern in the context of providing warnings in the event of a configuration error or problem. In some cases, tracking down variant configurations doesn't deserve a warning, because the variant configuration is a design feature.
Here's __init__.py for a some_algorithm package, which chooses an implementation based on the availability of the module files within the package:
try: from ...
Read now
Unlock full access