师奇怪 [英] Division oddity

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

问题描述

如果我从__future__ import division那么,那么eval(1/2)给我0.5预计

。但是,如果我打印输入(输入总和:)并输入1/2作为

,我得到的总和为0,好像我没有完成导入。我认为输入是

应该给出与原始输入的eval相同的行为 - 为什么这里的

差异?

If I do from __future__ import division then eval(1/2) gives me 0.5 as
expected. But if I do print input("enter a sum: ") and enter 1/2 as
the sum I get 0 as if I hadn''t done the import. I thought input was
supposed to give the same behaviour as an eval on raw input -- why the
difference here?

推荐答案

尝试1.0 / 2而不是1/2。请注意,当我做eval(1/2)时,我得到0而不是

0.5


-Denis


Le Sun,2004年1月11日23:45:48 +0000,Tim Rowe

< tim@remove_if_not_spam.digitig.co.uk> écrit:
Try 1.0/2 instead of 1/2. Note that when I do eval("1/2") I get 0 not
0.5

-Denis

Le Sun, 11 Jan 2004 23:45:48 +0000, Tim Rowe
<tim@remove_if_not_spam.digitig.co.uk> a écrit :
如果我从__future__ import division那么,那么eval(1/2)给我0.5作为
预期。但是如果我打印输入(输入总和:)并输入1/2作为
总和我得到0就好像我没有完成导入。我认为输入是否应该给出与原始输入的eval相同的行为 - 为什么
区别在这里?
If I do from __future__ import division then eval(1/2) gives me 0.5 as
expected. But if I do print input("enter a sum: ") and enter 1/2 as
the sum I get 0 as if I hadn''t done the import. I thought input was
supposed to give the same behaviour as an eval on raw input -- why the
difference here?





Denis Sarrazin写道:
Denis Sarrazin wrote:
尝试1.0 / 2而不是1/2。请注意,当我做eval(1/2)时,我得到0而不是
0.5


请注意:来自__future__ import division,请参阅PEP 238

改变了divisino运算符的行为。


我在Python 2.3.3中的结果:
Try 1.0/2 instead of 1/2. Note that when I do eval("1/2") I get 0 not
0.5
Please note: "from __future__ import division", see PEP 238 which
changes the behavior of the divisino operator.

My results with Python 2.3.3:
来自__future__进口部门
eval(''1/2'')
0.5输入()
1/2

0 eval(raw_input())
from __future__ import division
eval(''1/2'') 0.5 input() 1/2
0 eval(raw_input())



1/2

0.5

-Denis

Le Sun,2004年1月11日23:45:48 +0000,Tim Rowe
< tim@remove_if_not_spam.digitig.co.uk> écrit:


1/2
0.5

-Denis

Le Sun, 11 Jan 2004 23:45:48 +0000, Tim Rowe
<tim@remove_if_not_spam.digitig.co.uk> a écrit :

如果我从__future__ import division那么,那么eval(1/2)给我0.5作为
预期。但是如果我打印输入(输入总和:)并输入1/2作为
总和我得到0就好像我没有完成导入。我认为输入应该与原始输入上的eval具有相同的行为 - 为什么
区别在这里?
If I do from __future__ import division then eval(1/2) gives me 0.5 as
expected. But if I do print input("enter a sum: ") and enter 1/2 as
the sum I get 0 as if I hadn''t done the import. I thought input was
supposed to give the same behaviour as an eval on raw input -- why the
difference here?




Tim Rowe< tim@remove_if_not_spam.digitig.co.uk>写道:
Tim Rowe <tim@remove_if_not_spam.digitig.co.uk> writes:
如果我从__future__ import division那么,那么eval(1/2)给我0.5作为
预期。但是如果我打印输入(输入总和:)并输入1/2作为
总和我得到0就好像我没有完成导入。我认为输入是否应该给出与原始输入的eval相同的行为 - 为什么
区别在这里?
If I do from __future__ import division then eval(1/2) gives me 0.5 as
expected. But if I do print input("enter a sum: ") and enter 1/2 as
the sum I get 0 as if I hadn''t done the import. I thought input was
supposed to give the same behaviour as an eval on raw input -- why the
difference here?




输入函数从模块的上下文调用eval

其中''input''本身是定义的。如果你使用from __future__ import

division在模块A中并且具有打印3/2。在模块B中,模块B中值

3/2不应受输入的影响,因为模块B

可能取决于具有旧的整数除法行为。


乍一看结果有点令人惊讶,所以它可能应记录下来。



The input function is calling eval from the context of the module
where ''input'' itself is defined. If you use "from __future__ import
division" in module A and have "print 3/2" in module B, the value of
3/2 in module B shouldn''t be affected by the input, since module B
may depend on integer division having the old behavior.

The result is a little bit surprising at first glance though, so it
should probably be documented.


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

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