what is the jargon of the alias variable after imp

2019-08-04 23:58发布

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.

2条回答
forever°为你锁心
2楼-- · 2019-08-05 00:09

You will not be wrong if you call it asname.

enter image description here

查看更多
闹够了就滚
3楼-- · 2019-08-05 00:23

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.

查看更多
登录 后发表回答