博客
关于我
关于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/

你可能感兴趣的文章
NMF(非负矩阵分解)
查看>>
nmon_x86_64_centos7工具如何使用
查看>>
NN&DL4.1 Deep L-layer neural network简介
查看>>
NN&DL4.3 Getting your matrix dimensions right
查看>>
NN&DL4.7 Parameters vs Hyperparameters
查看>>
NN&DL4.8 What does this have to do with the brain?
查看>>
nnU-Net 终极指南
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>