avatarLaxfed Paulacy

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

1984

Abstract

pan>.<span class="hljs-built_in">pi</span>) # accessing the constant <span class="hljs-built_in">pi</span> from the <span class="hljs-built_in">math</span> <span class="hljs-built_in">module</span></pre></div><h2 id="204d">2. import <module> as <other_name></h2><p id="6c1d">With <code>import <module> as <other_name></code>, you import the entire namespace of <code><module></code> into the name <code><other_name></code>. This is useful when the module name is long and you wish to import an abbreviated version of it or if the module name clashes with an existing name in the calling module. Example:</p><div id="6dae"><pre><span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np <span class="hljs-built_in">print</span>(np.array([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>])) <span class="hljs-comment"># accessing the array function from the numpy module</span></pre></div><h2 id="83a5">3. from <module> import <name1>, <name2></h2><p id="ae4b">This variation allows you to import only specific names from the module. The names are added to the calling module’s namespace and can be accessed directly. Example:</p><div id="db7d"><pre><span class="hljs-keyword">from</span> datetime <span class="hljs-keyword">import</span> datetime, timedelta print(datetime.now()) # accessing the now() <span class="hljs-keyword">function</span> directly</pre></div><h2 id="cb14">4. from <module> import <name> as <other_name></h2><p id="0901">The fourth variation is a combination of the second and third, allowing you to import specific names from a module with an alternative name for each. Example:</p><div id="09e0"><pre><span class="hljs-keyword">from</span> <span class="hljs-keyword">statistics</span> <span class="hljs-keyword">import</span> mean <span class="hljs-keyword">as</span> average print(average([<span cla

Options

ss="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>])) # accessing the mean <span class="hljs-keyword">function</span> <span class="hljs-keyword">as</span> <span class="hljs-string">'average'</span></pre></div><h2 id="9007">Best Practices and Considerations</h2><ul><li><code>import <module></code> is the preferred approach as it keeps the imported module’s namespace completely separate from the calling module’s namespace.</li><li><code>import <module> as <other_name></code> is useful for long module names or to resolve naming clashes.</li><li>Importing specific names from a module should be done sparingly, as it removes them from the context of the calling module and can lead to loss of context and readability issues.</li></ul><p id="c8b3">It’s important to curate your namespace when importing modules to ensure clarity and maintainability of your code.</p><p id="a30a">In conclusion, understanding the various import statements and their best practices is crucial for writing maintainable and readable Python code.</p><p id="5d9f">Feel free to experiment with these import statements in your Python projects to gain a deeper understanding of their usage and benefits.</p><div id="73d6" class="link-block"> <a href="https://readmedium.com/python-renaming-imported-python-modules-cac2ed963342"> <div> <div> <h2>PYTHON — Renaming Imported Python Modules</h2> <div><h3>Computers are good at following instructions, but not at reading your mind. — Donald Knuth</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*7bGJ-sTqn8lTQ5gz.jpeg)"></div> </div> </div> </a> </div></article></body>

PYTHON — Import Statements in Python- A Comprehensive Summary

Simplicity is the soul of efficiency. — Austin Freeman

Import Statements in Python: A Comprehensive Summary

Import statements in Python are used to bring in modules or packages from external libraries or other Python files. This tutorial provides a comprehensive summary of different variations of import statements and their usage.

Variations of the import Statement

There are four variations of the import statement in Python. Let's examine each one with examples:

1. import <module>

When you use import <module>, you import the entire namespace of <module> into the name <module>. The imported module names can be accessed from the calling <module> with <module>.<name>. Here's an example:

import math
print(math.pi)  # accessing the constant pi from the math module

2. import <module> as <other_name>

With import <module> as <other_name>, you import the entire namespace of <module> into the name <other_name>. This is useful when the module name is long and you wish to import an abbreviated version of it or if the module name clashes with an existing name in the calling module. Example:

import numpy as np
print(np.array([1, 2, 3]))  # accessing the array function from the numpy module

3. from <module> import <name1>, <name2>

This variation allows you to import only specific names from the module. The names are added to the calling module’s namespace and can be accessed directly. Example:

from datetime import datetime, timedelta
print(datetime.now())  # accessing the now() function directly

4. from <module> import <name> as <other_name>

The fourth variation is a combination of the second and third, allowing you to import specific names from a module with an alternative name for each. Example:

from statistics import mean as average
print(average([1, 2, 3, 4, 5]))  # accessing the mean function as 'average'

Best Practices and Considerations

  • import <module> is the preferred approach as it keeps the imported module’s namespace completely separate from the calling module’s namespace.
  • import <module> as <other_name> is useful for long module names or to resolve naming clashes.
  • Importing specific names from a module should be done sparingly, as it removes them from the context of the calling module and can lead to loss of context and readability issues.

It’s important to curate your namespace when importing modules to ensure clarity and maintainability of your code.

In conclusion, understanding the various import statements and their best practices is crucial for writing maintainable and readable Python code.

Feel free to experiment with these import statements in your Python projects to gain a deeper understanding of their usage and benefits.

ChatGPT
Import
Statements
Summary
Comprehensive
Recommended from ReadMedium