python3.0不向后兼容啥意思_Python 3向后兼容性(shlex.quote与pipes.quote)

我的项目之一使用了shlex.quote从python 3.3开始可用的功能。但这shlex.quote与pipes.quote移至shlex之后已弃用的相同。现在为了兼容性,我使用以下代码:defcmd_quote(string):importsysifint(sys.version[2])<3:importpipesreturnpipes.quote(string)else:import...
摘要由CSDN通过智能技术生成

我的项目之一使用了shlex.quote从python 3.3开始可用的功能。但这shlex.quote与pipes.quote移至shlex之后已弃用的相同。

现在为了兼容性,我使用以下代码:

defcmd_quote(string):importsysifint(sys.version[2])<3:importpipesreturnpipes.quote(string)else:importshlexreturnshlex.quote(string)

是否存在更好的做法?

解决方案

首先,如果要对版本进行数值比较,请使用version_info,不要尝试解析version。*中的字符串。

这也意味着您可以利用元组的比较方式并编写如下代码:

ifsys.version_info

……如果Python达到3.10.0或4.0,它也不会中断。

*除非您需要担心1.5与2.0对比,否则在这种情况下,您显然已经在Usenet和StackOverflow之间建立了时间机器网关,并且可以肯定的是,您可以想到更好的用法。

无论如何,最好首先测试一下shlex.quote现有资源。这样,对读者来说您正在做什么很明显:shlex.quote如果可能,使用,pipes.quote如果不使用,则使用。

You'll see that pattern all over the place—even in the stdlib, where code is imported from C accelerator modules if possible, but fallback code is used if not (e.g., if you're usi

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值