suggestion: swap function in Python
nperkins7 at home.com
Thu May 31 15:19:37 EDT 2001
> 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...)
> LOAD_FAST b
> LOAD_FAST a
> STORE_FAST b
> STORE_FAST a
(Of course this might break any code that uses the name 'swap'...)
More information about the Python-list