Discover more from Daily Dose of Data Science
What Does The Google Styling Guide Say About Imports
Understanding the upsides and downsides.
Recently, I was reviewing Google’s Python style guide. Here’s what it says about imports.
When writing import statements, it is recommended to import the module instead of a function.
Quoting from the style guide:
Use import statements for packages and modules only, not for individual classes or functions.
Namespace clarity: Importing the module makes the source of the function clear during invocation. For instance,
my_functionis defined in
Fewer import statements: If you intend to use many functions from a specific module, importing each function may not be feasible, as shown below:
Explicitness: When you import a function instead of a module, it's immediately clear which functions you intend to use from the module.
Over to you: While the guideline is intended for Google's internal stuff, it may be applicable elsewhere. Yet, when would you still prefer importing functions over modules? Let me know :)
Read the full guide here: Google style guide.
Thanks for reading Daily Dose of Data Science! Subscribe for free to learn something new and insightful about Python and Data Science every day. Also, get a Free Data Science PDF (250+ pages) with 200+ tips.
👉 Tell me what makes this newsletter special for you by leaving a review here. It would mean the world to me :)
👉 If you liked this post, don’t forget to leave a like ❤️. It helps more people discover this newsletter on Substack and tells me that you appreciate reading these daily insights. The button is located towards the bottom of this email.
👉 If you love reading this newsletter, feel free to share it with friends!
Find the code for my tips here: GitHub.