Python 中导入模块后别名变量的行话是什么? [英] what is the jargon of the alias variable after import module in Python?

查看:49
本文介绍了Python 中导入模块后别名变量的行话是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python 生态系统中有很多包,例如 NumPy、Matplotlib.

There are lots of packages in Python ecosystem, like NumPy, Matplotlib.

为了简化编码,我们通常这样编码

to simplify coding, we usually code this way

import numpy as np

np 是别名、快捷方式或其他东西.

np is an alias, or shortcut, or something else.

问题是,这个用法的行话是什么?python doc 的链接会很棒.

the question is, what is the jargon of this usage? an link to python doc would be great.

推荐答案

导入是一种名称绑定;当前命名空间中的名称绑定到导入的对象.

Importing is a form of name binding; names in the current namespace are bound to imported objects.

import 声明文档称其为标识符,但标识符 名字.导入对象始终绑定到标识符,但 as <identifier> 语法允许您指定要使用的替代名称而不是默认名称.

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.

当将 Python 语法解析为抽象语法树时(这是 CPython 编译器所做的,您可以使用 ast 模块),那么生成的 ImportImportFrom 节点有 1 个或多个 名称,每个都是 ast.alias 类型的对象:

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)

alias类型有一个name和一个asname值,都是标识符,asname是可选的:

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.

这篇关于Python 中导入模块后别名变量的行话是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆