Wednesday, August 10, 2022

Import a Module or Object Under an Alias

The standard way of importing a module or an object adds it to Python’s mapping table, but Python also enables you to import a module or object under an alias of your choice. Using an alias can make your code more compact and easier to read.

Because you have not imported the module, you cannot refer to the object via the module. So if you have imported only the path object from the os module, you cannot use os.path to refer to it; you must use the unqualified path instead.

When you import a module or an object from a module, you can create an alias for the object. For example, the following statement imports the module acme_quants_derivatives and assigns the alias aqd:

import acme_quants_derivatives as aqd

You can then use the alias to refer to the module or object. For example, the following statement uses the aqd alias to refer to the ohlc() method in the acme_quants_derivatives module, assigning it to the variable n:

n = aqd.ohlc()

Similarly, you can use the from syntax to import an object from a module under an alias. The following example imports the version method from the platform module under the alias pv:

from platform import version as os_version

Likewise, you can then use the alias in your code. For example, the following statement uses the print() function to display the value of the method aliased as os_version:

print(os_version())

This statement returns information such as the following on a Mac:

Darwin Kernel Version 20.6.0: Mon Aug 30 06:12:21 PDT 2021;

root:xnu-7195.141.6~3/RELEASE_X86_64

Using an alias can be useful when you import multiple modules or objects that have the same name or names similar enough to be confusing. Using a shorter alias can also tighten and streamline your code.

List the Methods and Variables in a Module or Object

After importing a module or object, you can use Python’s dir() function to list the methods and variables it contains. For example, if you have imported acme_quants_derivatives and assigned the alias aqd, you can list the contents of aqd like this:

dir(aqd)

Python returns a list of the contents, such as the following:

['__builtins__', '__cached__', '__doc__', '__file__', '__init__', '__loader__',

'__name__', '__package__', '__spec__', 'export_weekly_stats', 'five_minute_

chart', 'import_daily_stats', 'ohlc', 'statbank', 'two_minute_chart']

The items whose names start and end with two underscores are built‐in Python methods. These are called dunder methods after the double underscore characters that precede and follow their names.

The items whose names do not use the double underscores, such as import_daily_stats and ohlc, are the methods and variables in the module or object.

You access the methods and variables through the alias of the imported object. For example, the following statement creates a variable named my_two_minute_chart and assigns to it the result of the two_minute_chart() method, which it accesses via the aqd alias:

my_two_minute_chart = aqd.two_minute_chart()

Reload a Module

Normally, you do not need to reload a module, because the Python interpreter does not unload modules. This means the only reason to reload a module is if it has changed since you first loaded it. While possible, such change in a loaded module is relatively rare.

To reload a module, first use the import command to import the importlib package:

import importlib

You can then use the reload() method of importlib to reload the module. For example, the following statement reloads the module named cust1:

importlib.reload(cust1)

Share:

0 comments:

Post a Comment