模块"builtin"的"__import__"属性的默认绑定是什么? [英] What is the default binding to the `__import__` attribute of the module `builtin`?

查看:181
本文介绍了模块"builtin"的"__import__"属性的默认绑定是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从Python入手

自定义进口商

Python提供的一项高级,很少需要的功能是 更改某些或所有导入和从中的语义的能力 声明.

An advanced, rarely needed functionality that Python offers is the ability to change the semantics of some or all import and from statements.

重新绑定__import __

您可以重新绑定模块builtin__import__属性 到您自己的自定义导入程序功能-例如,使用 内建的「Python内建程式」中显示的一般内建封装技术 第174页.

You can rebind the __import__ attribute of the module builtin to your own custom importer function—for example, one using the generic built-in-wrapping technique shown in "Python built-ins" on page 174.

  1. 在您可以重新绑定模块builtin__import__属性"中,模块builtin"应该改为模块builtins"吗?

  1. In "You can rebind the __import__ attribute of the module builtin", should "the module builtin" be "the module builtins" instead?

是模块builtin__import__属性"" __import__属性的默认实现?

Is "the __import__ attribute of the module builtin" bound to importlib.__import__function by default? Or does "the module builtin" provide the default implementation bound to its __import__ attribute?

推荐答案

  1. 是的,这是本书中的错字.在Python 2中,相同的模块命名为 __builtin__ (否s ),在Python 3中被命名为 builtins .

  1. Yes, that's a typo in the book. In Python 2 the same module is named __builtin__ (no s), in Python 3 it is named builtins.

builtins.__import__是与importlib.__import__不同的功能.如果要重新绑定builtins.__import__,请保存参考.

builtins.__import__ is a distinct function from importlib.__import__. If you are going to rebind builtins.__import__, save a reference.

  • builtins.__import__ is implemented in C, and essentially calls the C-API PyImport_ImportModuleLevelObject function.

importlib.__import__

importlib.__import__ is a pure-Python function. The goal of importlib is to provide a pure-python implementation of the import machinery so it can be hacked on more easily, and this function is no exception.

这篇关于模块"builtin"的"__import__"属性的默认绑定是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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