位置: 编程技术 - 正文

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

  • 建筑业预缴所得税怎么计算
  • 个人所得税申诉已受理多久出结果
  • 企业所得税从业人数怎么填,依据什么填写
  • 不征税收入有哪些类型
  • 装修费用摊销是当月还是次月
  • 专项附加扣除项目的6项具体规定有哪些
  • 预交所得税利润表怎么填
  • 境外人员为境内企业提供劳务如何缴税
  • 企业变更地址需要哪些资料
  • 公司举办的活动
  • 集团内部是什么意思
  • 票据权利的概念与性质
  • 转让法人要注意什么
  • 国税网不能打印,怎么设置
  • 策划费收费标准
  • 补充养老保险税收规定扣除率
  • 公司没有车牌可以买车吗
  • 残保金次年申报
  • 制造费用如何分摊到产品的成本
  • 新版edge浏览器文字不显示
  • 小规模购进商品只收到记账联专票
  • 公司的固定资产是什么
  • 误删的文件怎么撤回
  • 新电商行业
  • 最早在哪一年手机可以上网
  • 应付账款会计分离
  • phpstudy备份数据库
  • 夫妻房子归属权问题
  • nginx怎么搭建
  • 数据结构考试内容
  • 债权投资 科目
  • 民营企业的工业项目建设-经营-转让是什么模式
  • C++ Linux Web Server 面试基础篇-操作系统(四、线程通信)
  • 对外长期投资会计分录?
  • 保险佣金个人所得税怎么申报
  • 机动车价外费用发票如何开具
  • 一个月可以勾选多少进项税额
  • dedecms怎么改图片
  • Mongodb中MapReduce实现数据聚合方法详解
  • sql server数字类型
  • 会计核算方式有几种
  • 向农业生产者收购的原木 进项
  • 一般纳税人金税盘280怎么做账
  • 土地使用税是新征税吗
  • 捐赠支出税前扣除标准
  • 商业承兑汇票怎么贴现
  • 广告设计合同属于什么合同
  • 利润表的未分配利润是哪个
  • 人工工资会计科目
  • 营业外收支主要有哪些
  • 房屋出租要交的税有哪些
  • 私人边际成本怎么算
  • 商品库存明细账
  • 固定资产公司
  • 吸收的三种方式
  • sqlserver 索引
  • mysql5.7免安装版配置
  • 打开电脑windows
  • freebsd 安装
  • 图解在OS X中管理窗口大小的多种方法
  • ahqtb.exe是啥进程 ahqtb进程信息查询
  • shell中awk命令
  • 出现早搏就一辈子无法治愈吗
  • win7禁用开机启动服务
  • node.js 模块
  • HTML:scrollLeft,scrollWidth,clientWidth,offsetWidth完全详解
  • cocos2d安装教程
  • 菜鸟教程安卓下载
  • linux perl -e
  • jquery.upload.js
  • jquery mobile开发工具
  • python yield from 用法
  • Unity AssetBundle爬坑手记
  • python中字典怎么用
  • javascript入门·对象属性方法大总结
  • java教程
  • 安卓 触摸屏
  • javascript 类
  • Material Design:利用RecyclerView CardView实现新闻卡片样式
  • 车船税填表说明
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设