why the inconsistency?

John Hazen invalid at hazen.net
Tue Sep 23 21:33:31 EDT 2003


On Tue, Sep 23, 2003 at 06:04:33PM -0700, mensanator wrote:
> 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
> 
> 
> 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
> 
> Why is the "L" there? I thought "L" isn't used anymore?

I believe the L is there because the backticks invoke __repr__ 
intsead of __str__.  I think backticks are somewhat deprecated
now.  (At least Guido called them "a failed feature which never
got retired".)

I think what you want is 'str'.

>>> a = 2**64
>>> repr(a)
'18446744073709551616L'
>>> str(a)
'18446744073709551616'
>>> len(str(a))
20
>>>

-John
-- 
<my first name>@<my domain>





More information about the Python-list mailing list