位置: 编程技术 - 正文

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

发布时间:2024-02-27

推荐整理分享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参数)

  • 怎么计算土地增值税的相关税金
  • 财产行为税包括资源税吗
  • 税务是怎么回事
  • 股票初始计量后按新金融准则计入哪个科目
  • 征信高风险是什么意思
  • 法人工资怎么发最划算
  • 普票也要盖发票专用章吗
  • 公司清算存货怎么算
  • 支付广告roi
  • 附加税是当月计算吗
  • 车船税没有发票可以税前扣除吗
  • 固定资产不动产抵扣新政策
  • 收到股本的现金怎么做账
  • 咨询服务企业发展瓶颈期
  • 员工离职后收取客户钱款
  • 月结发票与实缴发票可以一起报销吗
  • 增值税发票税率6%是什么
  • 企业如何实现资源共享
  • 小规模季度30万免税怎么做账
  • 在建工程转入长期待摊费用吗
  • 安全生产费怎么算
  • 外资企业注销需要商务局备案吗
  • 如何使用u盘安装win11
  • linux CentOS/redhat 6.5 LVM分区使用详解
  • 收取违约金如何入账
  • 事业单位政府预算
  • u启动u盘怎么装系统
  • linux 管道命令详解
  • PHP:Memcached::setOption()的用法_Memcached类
  • php判断字符串长度的方法
  • ipad air3发布会价格
  • ai绘图素材
  • 固定资产非正常损失
  • 广告费和业务宣传费调增还是调减
  • 合伙企业分配股票给合伙人
  • php 计算
  • 网络应用开发技术
  • 世界上最完美的犯罪
  • vue预览word加水印
  • 内部结算属于什么科目类别
  • 财务费用汇兑损失是什么
  • 在发生以下的情况下,可判定货物
  • 发票章与开票方名称不一致是什么情况
  • 电子发票找不到了可以找回吗
  • 拆迁支出包括哪些
  • mysql数据库如何回滚
  • 其他收益算主营业务收入吗
  • 个税累计预扣法导致税交多了
  • sqlserver2008无法连接到local用户登录失败
  • 买车进项税
  • 红冲以前年度费用
  • 经营地址怎么变更
  • 往来会计岗位职责怎么写
  • sql查询速度慢如何优化
  • ubuntu怎么安装程序
  • linux常用命令top
  • centos8 固态硬盘
  • ubuntu zed
  • centos文本编辑器
  • win7系统无法修复怎么办
  • mac电脑安装windows
  • win7系统电脑图标不见了
  • linux apache2配置
  • 微软开始推送win10
  • 简单说明ssh远程登录流程
  • opengl画点
  • listview的item
  • jquery easy ui
  • shell脚本获取文件指定内容
  • JS中call/apply、arguments、undefined/null方法详解
  • javascript总结笔记
  • Developing for Android, IV: The Rules: Networking
  • android反编译软件
  • static function FindObjectsOfType (type : Type) : Object[]
  • jQuery+Ajax实现限制查询间隔的方法
  • jquery写网页
  • 建设工程需要缴纳哪些税
  • 北京供暖 2020
  • 税务登录 河北省地方税务局
  • 动漫企业的增值税率
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号