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.

Advantages:

  • Namespace clarity: Importing the module makes the source of the function clear during invocation. For instance, my_module.my_function() tells that my_function is defined in my_module.

  • Fewer import statements: If you intend to use many functions from a specific module, importing each function may not be feasible, as shown below:

Disadvantage:

  • 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.

👉 Read what others are saying about this post on LinkedIn and Twitter.

👉 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.

I like to explore, experiment and write about data science concepts and tools. You can read my articles on Medium. Also, you can connect with me on LinkedIn and Twitter.

Reply

or to participate.