检测__future__功能 [英] Detecting __future__ features

查看:95
本文介绍了检测__future__功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果某个特定功能已被

启用,那么将如何判断来自__future__ import thing声明?


例如,我可以这样做:


如果1/2 == 0:

打印正在使用的经典部门

其他:

打印正在使用的部门

我甚至可以这样做:


来自关键字import关键字

如果关键字(" with"):

print" with statement enabled"

else:

print" with statement not enabled"


(我并不特别关心相关功能是否已启用

通过显式调用导入,或者因为它已成为默认值。)


有没有一般机制?

-

史蒂文。

How would one tell at runtime if a particular feature has been enabled by
the "from __future__ import thing" statement?

For example, I can do this:

if 1/2 == 0:
print "classic division in use"
else:
print "true division in use"
I could even do this:

from keyword import keyword
if keyword("with"):
print "with statement enabled"
else:
print "with statement not enabled"

(I don''t especially care whether the feature in question has been enabled
via an explicit call to import, or because it has become the default.)

Is there any general mechanism?
--
Steven.

推荐答案

Steven D''Aprano< st *** @ REMOVE .THIS.cybersource.com.auwrote:
Steven D''Aprano <st***@REMOVE.THIS.cybersource.com.auwrote:

有没有一般机制?
Is there any general mechanism?



我只会使用预期的未来功能,如果结果不是我想要的b $ b(或者Python提出任何一种例外,比如使用关键字而不是

存在)我想我已经过去了:-)


-

Lawrence,oluyede.org - neropercaso.it

很难让男人理解

当他的工资取决于不是

了解它 - Upton Sinclair

I''d just use the expected future feature and if the result is not what I
expect (or Python raises any kind of exception, like using a keyword not
present) I''d think I''m in the past :-)

--
Lawrence, oluyede.org - neropercaso.it
"It is difficult to get a man to understand
something when his salary depends on not
understanding it" - Upton Sinclair


2007-07-30,Steven D''Aprano

< st *** @ REMOVE.THIS.cybersource .com.auwrote:
On 2007-07-30, Steven D''Aprano
<st***@REMOVE.THIS.cybersource.com.auwrote:

如果某个特定功能已经由来自__future__导入事物启用,那将如何判断某个特定功能是否已被b / b
启用;声明?
How would one tell at runtime if a particular feature has been
enabled by the "from __future__ import thing" statement?



我不明白资格,在运行时,你是
制作。只需导入你想要的东西并使用

它有什么问题?如果已经启用,则

导入声明不会造成任何伤害。


-

Neil Cerutti

互联网上的高速公路会变得更少吗? --George W. Bush

I don''t understand the qualification, "at runtime," you''re
making. What''s wrong with just importing what you want and using
it? If it''s already been enabled, no harm will come from the
import statement.

--
Neil Cerutti
Will the highways on the Internet become more few? --George W. Bush


7月30日上午9:39,Neil Cerutti< horp ... @ yahoo.comwrote:
On Jul 30, 9:39 am, Neil Cerutti <horp...@yahoo.comwrote:

2007-07-30,Steven D''Aprano


< st ... @ REMOVE.THIS.cybersource.com.auwrote:
On 2007-07-30, Steven D''Aprano

<st...@REMOVE.THIS.cybersource.com.auwrote:

如果某个特定功能已经由from __future__ import thing启用,那将如何判断某个特定功能是否已被b
启用?声明?
How would one tell at runtime if a particular feature has been
enabled by the "from __future__ import thing" statement?



我不理解资格,在运行时,你是
制作。只需导入你想要的东西并使用

它有什么问题?如果已经启用,

导入语句不会造成任何伤害。


I don''t understand the qualification, "at runtime," you''re
making. What''s wrong with just importing what you want and using
it? If it''s already been enabled, no harm will come from the
import statement.



我不是OP,所以也许我错过了他的意图。但是,我可以看到

是提出这个问题的一个很好的理由。


我似乎记得来自__future__ import声明只能在脚本开头完成
。如果你正在设计一个

模块意味着要导入,并且被其他程序使用,那么你b $ b无法控制?你不能使用'__future__ import"在你的

模块中。所以,你可能必须找到一种方法来弄清楚已经完成了什么。 (分区运算符给出的示例很好)。

$ b $bAndré

I''m not the OP, so perhaps I am missing his intent. However, I can see
a good reason for asking this question.

I seem to recall that the "from __future__ import" statement can only
be done at the beginning of a script. What if you are designing a
module meant to be imported, and used by other programs over which you
have no control? You can''t use "from __future__ import" in your
module. So, you may have to find a way to figure out what''s been
done. (the example given with the division operator is a good one).

André


-

Neil Cerutti

互联网上的高速公路会变得更少吗? --George W. Bush
--
Neil Cerutti
Will the highways on the Internet become more few? --George W. Bush



这篇关于检测__future__功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆