博客
关于我
关于from __future__ import absolute_import
阅读量:590 次
发布时间:2019-03-11

本文共 410 字,大约阅读时间需要 1 分钟。

__future__模块在Python中扮演着一个特殊的角色。它的出现是为了在旧版本的Python中支持新版本的模块和功能。这使得开发者能够在早期版本中测试新的模块是否兼容或可靠,同时保持代码的向后兼容性。

这项功能特别有用,当我们从包中导入模块时,例如在pkg/string.py文件中使用import string,如果没有absolute_import,则会优先查找包中的string模块。但如果使用absolute_import,它会从标准库中查找string模块。

接下来是absolute_import。它是argparse库中的一个重要组成部分,用于处理命令行参数的绝对路径导入。绝对导入与相对导入的区别在于前者会从当前包的顶层路径查找模块,而后者则会从当前包内部查找。

这两种导入方式的选择会显著影响代码的行为,尤其是在处理像string这样的常见模块时。理解它们的区别对于编写兼容性良好的代码至关重要。

转载地址:http://eyatz.baihongyu.com/

你可能感兴趣的文章
No resource identifier found for attribute 'srcCompat' in package的解决办法
查看>>
no session found for current thread
查看>>
no such file or directory AndroidManifest.xml
查看>>
No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
查看>>
NO.23 ZenTaoPHP目录结构
查看>>
no1
查看>>
NO32 网络层次及OSI7层模型--TCP三次握手四次断开--子网划分
查看>>
NOAA(美国海洋和大气管理局)气象数据获取与POI点数据获取
查看>>
NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata
查看>>
node exporter完整版
查看>>
Node JS: < 一> 初识Node JS
查看>>
Node JS: < 二> Node JS例子解析
查看>>
Node Sass does not yet support your current environment: Windows 64-bit with Unsupported runtime(72)
查看>>
Node 裁切图片的方法
查看>>
Node+Express连接mysql实现增删改查
查看>>
node, nvm, npm,pnpm,以前简单的前端环境为什么越来越复杂
查看>>
Node-RED中Button按钮组件和TextInput文字输入组件的使用
查看>>
vue3+Ts 项目打包时报错 ‘reactive‘is declared but its value is never read.及解决方法
查看>>
Node-RED中Switch开关和Dropdown选择组件的使用
查看>>
Node-RED中使用html节点爬取HTML网页资料之爬取Node-RED的最新版本
查看>>