Re: [Tutor] Variable Swap

2007-01-30 Thread Kent Johnson
Steve Nelson wrote: >> x, y = y, x > > Doesn't this use temporary variables? Yes, behind the scenes maybe it does. It may create a temporary list, then unpack it - that is what the syntax actually means. Let's take a look: In [1]: import dis In [2]: def f(x, y): ...: x, y = y, x ..

Re: [Tutor] Variable Swap

2007-01-30 Thread Simon Brunning
On 1/30/07, Steve Nelson <[EMAIL PROTECTED]> wrote: > > x, y = y, x > > Doesn't this use temporary variables? Python doesn't really *have* variables, as such, so no. What it does is to create a tuple referring to the objects (or just possibly one object) referred to by the names 'y' and 'x', then

Re: [Tutor] Variable Swap

2007-01-30 Thread Steve Nelson
On 1/29/07, Kent Johnson <[EMAIL PROTECTED]> wrote: > Because 2 ^ 3 == 1, right? Are you sure you understand what xor does? It > is a bitwise exclusive or: Yes... at a binary level, it returns true if either input is true, but not both: A B Q 0 0 0 0 1 1 1 0 1 1 1 0 Thus it has the effect of swa

Re: [Tutor] Variable Swap

2007-01-29 Thread Kent Johnson
Steve Nelson wrote: > Hello, > > I understand the use of xor to do a variable swap without a temporary > variable: > x=2 y=3 y=x^y x=x^y y=x^y x > 3 y > 2 > > > However, why do I see this? > y=x^y y > 1 Because 2 ^ 3 == 1, right? Are you sure you

[Tutor] Variable Swap

2007-01-29 Thread Steve Nelson
Hello, I understand the use of xor to do a variable swap without a temporary variable: >>> x=2 >>> y=3 >>> y=x^y >>> x=x^y >>> y=x^y >>> x 3 >>> y 2 However, why do I see this? >>> y=x^y >>> y 1 S. ___ Tutor maillist - Tutor@python.org http://mail