suggestion: swap function in Python

D-Man dsh8290 at rit.edu
Thu May 31 15:49:08 EDT 2001


On Thu, May 31, 2001 at 07:19:37PM +0000, Nick Perkins wrote:
| 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'...)

Why add that to the language?  It should be a feature of the compiler.
As Skip showed, if the compiler sees

    <var1> , <var2> = <var2> , <var1>

it can generate that same bytecode without adding YAK (Yet Another
Keyword) to the language.  Keep the language small and simple (yet
complete) and make the compiler smarter.

Those bytecodes are still not atomic (there are 4 instructions above)
so adding the keyword like that doesn't help in the case of threading.

-D





More information about the Python-list mailing list