位置: 编程技术 - 正文

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参数)

  • 破产企业发放工资要不要纳税?
  • 进项发票已认证,发现发票有问题怎么办
  • 股东所有者权益公式
  • 房地产房屋质量检测
  • 生产车间领用材料,用于产品生产
  • 电子发票打小了可以报销吗
  • 城建税借贷方向
  • 进项税额转出是在借方还是贷方
  • 工业企业销售成本如何计算
  • 企业所得税汇算清缴账务处理
  • 保险没有合同能退保吗
  • 记账凭证广告费
  • 公司汽车装潢和保养做什么会计科目
  • 过路费是地税还是国税
  • 装修公司开发票需要客户承担税点吗?
  • 实缴年月正常补收
  • 发票虚开税务局要求补税怎么办?
  • 退税勾选后怎么申报
  • 已开票怎么做坏账处理
  • 个人向公司借贷需要交税吗
  • 个体户营业收入超过500万
  • 自产房产部分出租后的房产税怎么交?
  • 应交税费核算
  • 出口退税过期了怎么办理
  • 华为鸿蒙系统怎么降级版本
  • 如何免费获取Win11企业版
  • 不起眼的暴利小生意农村
  • php定义静态变量的关键字
  • 个税手续费会计分录
  • 外购固定资产入账
  • 转出未交增值税会计科目
  • linux去除文本中的特殊符号
  • 股票收益的计算公式
  • 库存股的会计科目
  • 收到业绩补偿会扣税吗
  • ensmix32.exe进程安全吗 ensmix32进程是什么文件产生的
  • 目前超频最高几ghz
  • 怎么用html做一个收藏夹
  • 安装人员食宿费谁承担
  • 减免增值税会计处理
  • 旅行社差额征税税率是多少
  • 业务宣传费列支合理性
  • webpack与gulp面试题
  • 小帽科技
  • thinkphp 多表查询
  • 打印的机票行程怎么查
  • 桥接模式例子
  • 企业所得税成本费用一定要发票的吗
  • 申报的财务报表在哪里查询
  • 深入浅出夏寒陆行全文免费阅读笔趣阁番外
  • sqlserver怎么清空表数据
  • 不符合资本化的长期借款利息计入什么科目
  • 房地产企业拆迁补偿费入账要求
  • 申报成功后怎么更正申报
  • 不抵扣进项税额转出怎么做分录处理
  • 企业收到宣传费怎么做账
  • 建筑企业收挂靠公司的管理费如何做账?
  • 土地出让金如何核算
  • 应付账款现金折扣会计分录
  • 逾期的押金计入什么科目
  • 货物发生退运了怎么办
  • 以前年度损益调整结转到本年利润吗
  • 增值税专用发票丢了怎么补救
  • 银行对账单怎么打印
  • 应收账款余额等于什么?
  • 存储过程的定义及调用方法
  • mysql优化sql有哪些
  • 利用系统的公文有哪些
  • ubuntu怎样调出命令行
  • 如何关闭win8的windows defender
  • c盘越来越小了
  • linux 文件数量 命令
  • javascript date format
  • bat批量命名
  • 小助手是什么软件
  • js判断鼠标位置是否在元素区域内使用
  • js每天的定时任务
  • Unity MeshBaker 合并网格和材质
  • jQuery插件Flexslider实现图片轮播、图文结合滑动切换效果
  • 工会经费申报表在哪里查询
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设