moving items in a list
sismex01 at hebmex.com
sismex01 at hebmex.com
Fri Jan 10 16:48:36 EST 2003
More information about the Python-list mailing list
Fri Jan 10 16:48:36 EST 2003
- Previous message (by thread): moving items in a list
- Next message (by thread): moving items in a list
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
> From: andy [mailto:andy at eastonwest.co.uk] > Sent: Friday, January 10, 2003 3:42 PM > > And that was, er, about 20 years ago. I've never seen a > language that could > do that any better without a 'swap' function. > BUT it's not a *true* swap... more to follow. > > a,b=b,a # how much more obvious can you get? > Indeed. :-) > Just goes to show that you need to talk to people about > Python - just applying what you already know from (even > 20+ years of) programming experience IS NOT ENOUGH. That > would never have occurred to me... > > -andyj > In this form ("a,b = b,a"), what actually happens behind curtains is that, first, a tuple is created with (b,a), and then, that same (temporary) tuple is unpacked into (a,b). It might take more time to do this operation, than to do something like: _ = a a = b b = _ where "_" is some temporary variable. Alas, there's no free lunch. Although, it might be possible that an optimizer might recognize cases such as these (pack-unpack) and optimize it away into two sequential STORE_FASTs, because the object references are already in the stack. So, now, it does a LOAD_FAST (b), LOAD_FAST (a), CREATE_TUPLE, UNPACK_SEQUENCE, STORE_FAST (a), STORE_FAST (b). Where, it might be able to skip the CREATE_TUPLE/UNPACK_SEQUENCE pair and simply LOAD_FAST / LOAD_FAST / STORE_FAST / STORE_FAST, much better, no? Anywho, that's life for ya. :-) -gustavo
- Previous message (by thread): moving items in a list
- Next message (by thread): moving items in a list
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list