What is a function parameter =[] for?

BartC bc at freeuk.com
Tue Nov 24 16:14:12 EST 2015


On 24/11/2015 20:54, Antoon Pardon wrote:
> Op 24-11-15 om 20:15 schreef Ian Kelly:
>
>>> But no matter what you want to call it. The dis module shows that
>>> -42 is treated in exactly the same way as 42, which is treated
>>> exactly the same way as () or as (5, 8, 13) which is treated
>>> differently from [] or [5, 8, 13].
>>
>> This is an implementation detail. The compiler would also be free to
>> compile -42 into byte code as the negation of the constant 42. That 42
>> is a literal, on the other hand, is part of the language
>> specification.
>
> I think you are picking nits. Sure the byte code could compile -42 into
> a byte code for 42 and a negation. Just as it could compile 42 into byte
> code for adding 32, 8 and 2.
>
> The point is, that the reverse isn't true. It couldn't compile [5, 8, 13]
> into a LOAD_CONST.

I think it can, with a couple of extra instructions:

     LOAD_GLOBAL 0 (list)
     LOAD_CONST  1 ((5, 8, 13))
     CALL_FUNCTION

ie. by converting a tuple to a list, but it can also be done with a 
special byte-code. The effect is a that a constant list can be 
constructed without having to evalate each element at runtime, which I 
assume would be less efficient if the list is big enough (perhaps not 
for just 3 elements).

-- 
Bartc






More information about the Python-list mailing list