suggestion: swap function in Python
Roman Suzi
rnd at onego.ru
Thu May 31 15:48:44 EDT 2001
On Thu, 31 May 2001, Nick Perkins wrote:
>> A swap function is impossible in Python. A function cannot rebind names in
>> the caller's namespace. This is why del is a statement and not a function,
>> for instance.
>>
>> --
>> Remco Gerlich
>
>Agreed. It is impossible for a function,
>but what if it was a statement?
>
>Given the tiny amount of bytecode, it should be very easy to implement.
>
>suggested syntax for a 'swap' statement:
>
>a = 10
>b = 20
>swap a b
>
>( no parens, just like print, del, import...)
>
>resulting in:
>> LOAD_FAST b
>> LOAD_FAST a
>> STORE_FAST b
>> STORE_FAST a
>
>(Of course this might break any code that uses the name 'swap'...)
this instead of:
24 LOAD_FAST 1 (b)
27 LOAD_FAST 0 (a)
30 BUILD_TUPLE 2
33 UNPACK_TUPLE 2
36 STORE_FAST 0 (a)
39 STORE_FAST 1 (b)
for more general and powerful:
a, b = b, a
I think, it could be better if there were some code
optimizer for Python to turn
BUILD_TUPLE
UNPACK_TUPLE
into
NOP
Sincerely yours, Roman Suzi
--
_/ Russia _/ Karelia _/ Petrozavodsk _/ rnd at onego.ru _/
_/ Thursday, May 31, 2001 _/ Powered by Linux RedHat 6.2 _/
_/ "You have to be sharp to be on the cutting edge." _/
More information about the Python-list
mailing list