Tail recursion to while iteration in 2 easy steps

MRAB python at mrabarnett.plus.com
Wed Oct 2 21:46:53 EDT 2013


On 03/10/2013 02:39, Dave Angel wrote:
> On 2/10/2013 21:24, Steven D'Aprano wrote:
>
>> On Wed, 02 Oct 2013 18:17:06 -0400, Terry Reedy wrote:
>>
>>> CPython core developers have be very conservative about what
>>> tranformations they put into the compiler. (1,2,3) can always be
>>> compiled as a constant, and so it is. [1,2,3] might or might not be a
>>> constant, depending on the context, and no attempt is made to analyze
>>> that.
>>
>> The first sentence of this is correct. The next two don't quite make
>> sense to me, since I don't understand what you mean by "constant" in this
>> context. I *think* you might be referring to the LOAD_CONST byte-code,
>> which in Python 3.3 understands tuples like (1, 2, 3), but not lists. So
>> a literal (1, 2, 3) gets created at compile-time with a single LOAD_CONST
>> call:
>>
>> py> from dis import dis
>> py> dis(compile("x = (1, 2, 3)", '', 'exec'))
>>   1           0 LOAD_CONST               4 ((1, 2, 3))
>>               3 STORE_NAME               0 (x)
>>               6 LOAD_CONST               3 (None)
>>               9 RETURN_VALUE
>>
>>
>> while a literal [1, 2, 3] does not:
>>
>>
>
> The difference is that a tuple can be reused, so it makes sense for the
> comiler to produce it as a const.  (Much like the interning of small
> integers)  The list, however, would always have to be copied from the
> compile-time object.  So that object itself would be a phantom, used
> only as the template with which the list is to be made.
>
The key point here is that the tuple is immutable, including its items.




More information about the Python-list mailing list