Fix the Broken __all__ Export
Intermediate · Fix the bug · from How to Create and Structure a Python Package
From the lesson: How to Create and Structure a Python Package
Imagine you have a filing cabinet. Right now, all your papers are thrown in one big drawer - recipes mixed with homework mixed with tax forms. Finding anything is a nightmare.
Read the lessonThe code below defines a simulated package with a __all__ list and three functions. The get_public_api() function should return only the names listed in __all__, but there are bugs. Fix them so the output shows exactly the two public functions.
# Simulated package exports
_all_ = ['calculate_area', 'calculate_perimeter']
def calculate_area(radius):
return 3.14159 * radius ** 2
def calculate_perimeter(radius):
return 2 * 3.14159 * radius
def _internal_validate(radius):
return radius > 0
def get_public_api():
all_functions = ['calculate_area', 'calculate_perimeter', '_internal_validate']
public = []
for func in all_functions:
if func in _all_:
public.append(func)
return public
result = get_public_api()
for name in result:
print(name)
More Package exercises
- Build a Package Structure DictionaryBeginner
- Predict the Import PathIntermediate
- Refactor Into a Package RegistryAdvanced
All Python exercises · 50 practice problems · Modules & Packaging quiz