Join us and get access to thousands of tutorials and a community of expert Pythonistas.
This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.
Revisiting the shapes Package
00:00
And there’s one more thing that I want to show you that __all__ is really useful for. Going back to packages, you can use __all__ to expose specific objects from the package’s modules at the package level.
00:11
So to revisit the shapes package’s __init__, delete this. You can first make a couple imports to bring classes to the package level.
00:20
from shapes.circle import Circle, and from shapes.square import Square.
00:25
And now you can expose them via __all__. __all__ equals a list of the strings, "Circle" and "Square".
00:33
And because these are the class names you just imported, make sure that it’s capital C Circle and capital S Square. Otherwise, you’re only exposing the modules and not the classes. Hit save and open the REPL.
00:46
from shapes import *, and run dir(). And the Circle and Square classes are brought directly into the global namespace.
00:57 And you can confirm they are the classes, not the modules.
01:01
This way, you can organize key imports at the package level, which for certain packages can make for a very clean interface. Next up, some best practices and pitfalls to avoid when using __all__.
Become a Member to join the conversation.