Reputation:
There are lots of packages in Python ecosystem, like NumPy, Matplotlib.
to simplify coding, we usually code this way
import numpy as np
np is an alias, or shortcut, or something else.
the question is, what is the jargon of this usage? an link to python doc would be great.
Upvotes: 1
Views: 224
Reputation: 1123420
Importing is a form of name binding; names in the current namespace are bound to imported objects.
The import
statement documentation calls it an identifier, but identifiers are names. Importing an object always binds to an identifier, but the as <identifier>
syntax lets you specify an alternate name to use instead of the default.
When parsing Python syntax into an Abstract Syntax Tree (which is what the CPython compiler does, and you can do with the ast
module), then the resulting Import
and ImportFrom
nodes have 1 or more names
, each an object of the ast.alias
type:
| Import(alias* names)
| ImportFrom(identifier? module, alias* names, int? level)
and the alias
type has a name
and an asname
value, both identifiers, and asname
is optional:
-- import name with optional 'as' alias.
alias = (identifier name, identifier? asname)
So they are just names, variables, and because they differ from the default for those imports, it's fine to call them aliases.
Upvotes: 2