为什么不一致? [英] why the inconsistency?

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

问题描述

我刚刚安装了Python 2.3(从2.1升级)。


版本2.3明显更快,并且自动转换为长整数非常方便:

I just installed Python 2.3 (upgrading from 2.1).

Version 2.3 is noticably faster and the automatic
conversion to long integers is very handy:

print 2 ** 64
18446744073709551616

但如果我想要要知道2 ** 64有多少位数,我不能

只做

print len(2 ** 64)
Traceback(最近一次)最后调用):

文件"<交互式输入>",第1行,在?

TypeError:未确定对象的len()


所以相反,我做了这个

print len(`2 ** 64`)
21


但是正确答案是20,而不是21.

答案错误的原因

打印`2 ** 64`
print 2**64 18446744073709551616
But if I want to know how many digits 2**64 has, I can''t
just do
print len(2**64) Traceback (most recent call last):
File "<interactive input>", line 1, in ?
TypeError: len() of unsized object

So instead, I did this
print len(`2**64`) 21

But the correct answer is 20, not 21. The reason the
answer is wrong
print `2**64`



18446744073709551616L


为什么L为L。那里?我以为L是不是已经用完了?


18446744073709551616L

Why is the "L" there? I thought "L" isn''t used anymore?

推荐答案

2003年9月23日18:04:33 -0700,mensanator写道:
On 23 Sep 2003 18:04:33 -0700, mensanator wrote:
print len(`2 ** 64`)21

但正确的答案是20,而不是21.原因<答案是错的
打印`2 ** 64`
print len(`2**64`) 21

But the correct answer is 20, not 21. The reason the
answer is wrong
print `2**64`


18446744073709551616L

为什么L指的是L。那里?我以为L不再使用?


18446744073709551616L

Why is the "L" there? I thought "L" isn''t used anymore?




您想要执行两项操作;明确表示。


#将数字转换为字符串表示

foo ="%d" %(2 ** 64)

#获取字符串长度

打印len(foo)


因为你有指出,一个数字没有固有的字符串表示

(因而没有长度);明确地选择一个(例如通过字符串格式化)。


-

\我前几天买了一只狗。我把他命名为Stay。它很有趣|

` \给他打电话。 来吧,住!来吧,住!他疯了。 |

_o__)现在他只是忽略了我并继续打字。 - Steven Wright |
Ben Finney< http://bignose.squidly.org/>



You want to perform two operations; make them explicit.

# Convert number to string representation
foo = "%d" % ( 2**64 )
# Get length of string
print len( foo )

As you''ve pointed out, a number has no inherent string representation
(and thus no length); choose one explicitly (e.g. by string formatting).

--
\ "I bought a dog the other day. I named him Stay. It''s fun to |
`\ call him. ''Come here, Stay! Come here, Stay!'' He went insane. |
_o__) Now he just ignores me and keeps typing." -- Steven Wright |
Ben Finney <http://bignose.squidly.org/>


2003年9月23日18: 04:33 -0700,mensanator写道:
On 23 Sep 2003 18:04:33 -0700, mensanator wrote:
print'2 ** 64`
print `2**64`


18446744073709551616L

为什么L为L。那里?我以为L不再使用了?


18446744073709551616L

Why is the "L" there? I thought "L" isn''t used anymore?




直接回答这个问题:因为你已经要求结果


repr(2 ** 64)


,正如所示,它向您显示该数字存储为

长整数(自重​​新开始以来) ()是为了向您显示关于

对象类型及其值的信息。


我相信`foo`语法已被弃用赞成repr(foo),没有?

如果你使用了repr(),也许这个假设会变成明显的b $ b。

-

\在狗的外面,一本书是男人最好的朋友。在一个|

` \狗的里面,它太黑了,无法阅读。 - Groucho Marx |

_o__)|

Ben Finney< http://bignose.squidly.org/>



To answer this directly: because you''ve asked for the result of

repr( 2**64 )

which, as demonstrated, is showing you that the number is stored as a
long integer (since the point of repr() is to show you information about
the type of the object as well as its value).

I believe the `foo` syntax is deprecated in favour of repr( foo ), no?
If you''d used repr(), perhaps the assumption would have become
apparent.

--
\ "Outside of a dog, a book is man''s best friend. Inside of a |
`\ dog, it''s too dark to read." -- Groucho Marx |
_o__) |
Ben Finney <http://bignose.squidly.org/>




" mensanator" <我******** @ aol.com>在消息中写道

news:fb ************************** @ posting.google.c om ...

"mensanator" <me********@aol.com> wrote in message
news:fb**************************@posting.google.c om...
我刚刚安装了Python 2.3(从2.1升级)。

版本2.3速度明显更快,并且自动转换为长整数非常方便:
I just installed Python 2.3 (upgrading from 2.1).

Version 2.3 is noticably faster and the automatic
conversion to long integers is very handy:
print 2 ** 64 18446744073709551616

但是如果我想知道2 ** 64有多少位数,我可以''只需要
打印len(2 ** 64)Traceback(最近一次调用最后一次):
文件"< interactive input>",第1行,在? TypeError:未确定对象的len()

所以相反,我做了这个
打印len(`2 ** 64`)21

但正确的答案是20,而不是21.
答案错误的原因
打印`2 ** 64`
print 2**64 18446744073709551616
But if I want to know how many digits 2**64 has, I can''t
just do
print len(2**64) Traceback (most recent call last):
File "<interactive input>", line 1, in ?
TypeError: len() of unsized object

So instead, I did this
print len(`2**64`) 21

But the correct answer is 20, not 21. The reason the
answer is wrong
print `2**64`


18446744073709551616L

为什么L表示L。那里?我以为L是不是已经用完了?


18446744073709551616L

Why is the "L" there? I thought "L" isn''t used anymore?




我相信它会在2.4中消失,但我可能会因为这个而错误。


John Roth



I believe it''s scheduled to vanish in 2.4, but I could
be wrong about that.

John Roth


这篇关于为什么不一致?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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