l = range(int(1E9))

Cecil Westerhof Cecil at decebal.nl
Fri May 1 01:13:43 EDT 2015


Op Friday 1 May 2015 06:42 CEST schreef Steven D'Aprano:

> On Fri, 1 May 2015 02:06 am, Cecil Westerhof wrote:
>
>> If I execute:
>> l = range(int(1E9)
>
>
> Others have already answered your questions about memory. Let me
> answer the question you didn't ask about style :-)

That can be very useful.


> Don't use "l" as a variable name, as it looks too much like 1.
> Better to use L, or even better, a meaningful name.
>
> Rather than convert a float 1E9 to an int at runtime, better to use
> an int:
>
> range(10**9)
>
>
> With recent versions of CPython, the compiler has a keyhole
> optimiser which does constant folding. For implementations of Python
> which don't do constant folding, 10**9 is likely to be faster than
> int(1E9) -- but even if it isn't, does it matter? It will be very
> fast one way of the other. The important thing is that 10**9
> expresses the intention to use an integer in a more direct fashion
> than using 1E9.

It was just a short hack to show the problem. In real code I would
never use l. But I'll remember to use 10**9 instead of 1E9.

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof



More information about the Python-list mailing list