位置: 编程技术 - 正文

Python内置函数——__import__ 的使用方法(python内置函数什么用来返回序列中的最大元素)

编辑:rootadmin

推荐整理分享Python内置函数——__import__ 的使用方法(python内置函数什么用来返回序列中的最大元素),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:python内置函数open()的参数用来指定打开文本文件,查看变量类型的python内置函数,python内置函数用来返回列表,元组,字典,python内置函数用来打开或创建文件并返回文件对象,python内置函数用来返回列表,元组,字典,python内置函数可以返回列表 元组 字典 的元素个数,python内置函数open()的参数用来指定打开文本文件,python内置函数大全表,内容如对您有帮助,希望把文章链接给更多的朋友!

__import__() 函数用于动态加载类和函数 。

如果一个模块经常变化就可以使用 __import__() 来动态载入。

语法

__import__ 语法:

__import__(name[, globals[, locals[, fromlist[, level]]]])

参数说明:

name -- 模块名

英文文档:

__import__(name, globals=None, locals=None, fromlist=(), level=0)

This function is invoked by the import statement. It can be replaced (by importing the builtins module and assigning to builtins.__import__) in order to change semantics of the import statement, but doing so is strongly discouraged as it is usually simpler to use import hooks (see PEP ) to attain the same goals and does not cause issues with code which assumes the default import implementation is in use. Direct use of __import__() is also discouraged in favor of importlib.import_module().

The function imports the module name, potentially using the given globals and locals to determine how to interpret the name in a package context. The fromlist gives the names of objects or submodules that should be imported from the module given by name. The standard implementation does not use its locals argument at all, and uses its globals only to determine the package context of the import statement.

level specifies whether to use absolute or relative imports. 0 (the default) means only perform absolute imports. Positive values for level indicate the number of parent directories to search relative to the directory of the module calling __import__() (see PEP for the details).

When the name variable is of the form package.module, normally, the top-level package (the name up till the first dot) is returned, not the module named by name. However, when a non-empty fromlist argument is given, the module named by name is returned.

Python内置函数——__import__ 的使用方法(python内置函数什么用来返回序列中的最大元素)

说明:

  1. 函数功能用于动态的导入模块,主要用于反射或者延迟加载模块。

  2. __import__(module)相当于import module

先定义两个模块mian.py和index.py,两个文件在同一目录下:

执行main.py,可以证实动态加载了index.py,__import__返回的模块也是index模块

3. __import__(package.module)相当于from package import name,如果fromlist不传入值,则返回package对应的模块,如果fromlist传入值,则返回package.module对应的模块。

先定义archives包,其中包含user和role两个模块:

结构如下:

修改mian.py:

执行main.py,可以证实动态加载了archives包,__import__返回的模块也是archives模块

C:UsersAdminDocumentsPython3importtest>python main.pymainarchives.__index__hello archivesTraceback (most recent call last): File "main.py", line 5, in <module> archives.userAttributeError: module 'archives' has no attribute 'user'

修改mian.py:

执行main.py,可以证实动态加载了archives包的user模块,__import__返回的模块也是archives模块

修改mian.py:

执行main.py,可以证实动态加载了archives包的user模块,__import__返回的模块是user模块

4. level参数,指定是使用绝对导入还是相对导入。 0(默认值)表示只执行绝对导入。

标签: python内置函数什么用来返回序列中的最大元素

本文链接地址:https://www.jiuchutong.com/biancheng/375434.html 转载请保留说明!

上一篇:Django中login_required装饰器的深入介绍

下一篇:python中print()函数的“,”与java中System.out.print()函数中的“+”功能详解(python中print函数的end参数)

  • 息税前利润变动率的计算公式
  • 开分公司赚钱吗
  • 企业级软件开发
  • 残保金申报表中的上年工资总额指所属期的上一年吗
  • 行政单位工资支出标准
  • 企业合并的被合并方会计分录
  • 机票的差额发票可以报销吗
  • 管理费用中的五项费用
  • 免抵调增值税是否缴纳附加税
  • 房产税税率2023税率表
  • 权责发生制如何计算
  • 小企业 企业所得税
  • 融资租赁到期后资产归属
  • 税务异常不处理会的后果
  • 开具发票必须附列清单吗
  • 公司哪些发票可以抵税
  • 禁止局域网用户访问权限
  • 苹果11屏幕尺寸长宽
  • macqq怎么查看删除好友
  • php红包源码
  • 招财树的养殖方法
  • linux chcon命令
  • 承租人对经营租赁的会计处,怎么快速记住方法
  • 营业利润,利润总额的计算公式是
  • powershell.exe什么意思
  • 出口退税科目 月底如何结转
  • html visit
  • 外币借款费用
  • vue鼠标点击事件点击改变效果,再次点击恢复效果
  • 工会福利如何做账
  • 酒吧送酒的人叫什么
  • 增值税报表填报说明
  • 冲销暂估入账应该填什么凭证
  • vue解决异步取值问题
  • 内存分配理解是什么
  • 充电桩折旧年限是多少年
  • python中numpy数组和列表的区别
  • 火车票抵税申报表怎么填
  • 维修费的会计分录怎么写
  • python中myqr的用法
  • 织梦建站详细教程
  • 员工探亲路费报销入哪个科目
  • 公司车转个人名下车牌需要换吗
  • 专项附加扣除哪种申报方式好
  • mysql相关
  • 广告赞助支出可以抵税吗
  • 融资租赁手续费进项税能抵扣吗
  • 主办会计的工作内容和职责
  • 结转损益主营业务收入在借方
  • 未税金额怎么算税费
  • 查账征收个体户经营所得税怎么计算
  • 坏账准备的计提分录
  • 企业合并会计处理案例
  • 财务费用利息收入的账务处理
  • 私车公用是否违法
  • 外购材料并提供建筑服务
  • 法人投资属于什么会计科目
  • 质量赔款能冲减收入么
  • 当月发票已认证还能作废吗
  • 公司劳务过账的法律依据
  • 低值易耗的管理制度
  • 公司营运成本
  • linux的lvm扩容
  • ubuntu nm
  • linux查看磁盘空间大小以及分配
  • win7手动更新在哪里设置
  • lnmgr.exe是什么
  • win7禁用administrator
  • windows无法预览文件
  • ExtJS如何设置与获取radio控件的选取状态
  • javascript模块化规范有哪些
  • NGUI之UIGrid
  • 音频焦点图广告
  • 手机gpu过度绘制
  • 使用驱动器u盘之前需要格式化
  • nodejs的流处理模块
  • javascript高级程序设计电子书
  • jquery获取文件大小
  • jquery全局变量和局部变量
  • 平顶山属于河南还是河北
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设