how to get the ordinal number in list

Rustom Mody rustompmody at gmail.com
Mon Aug 11 02:22:45 EDT 2014


On Monday, August 11, 2014 11:16:25 AM UTC+5:30, Chris Angelico wrote:
> On Mon, Aug 11, 2014 at 3:23 PM, Rustom Mody wrote:
> > A C programmer asked to swap variables x and y, typically writes something like
> > t = x; x = y; y = t;
> > Fine, since C cant do better.
> > But then he assumes that that much sequentialization is inherent to the problem...
> > Until he sees the python:
> > x,y = y,x
> > The same applies generally to all programmers brought up on imperative style.

> Uhh, that's still imperative. There is a chronological boundary here:
> prior to that statement's execution, x and y have certain values, and
> after it, they have the same values but the other way around. When
> you're manipulating algebraic statements, moving down the page doesn't
> correspond to any sort of time change, which is why "x = x + 1" has no
> solutions.

> Please explain to me how "x,y = y,x" is materially different from "x =
> x + 1" in that the latter is, in your words, an abomination, but you
> say the former doesn't have the same problem.

I was giving an analogy for a mindset that

- is sequencing hungup
- is not aware of being hungup

until a more abstract solution is presented.

The two -- x=x+1 and the swap -- are not related.
And yes the swap is imperative but less so than the C
because it elides the fact that an implementation of x,y = y,x
could be
- t=x;x=y;y=t			# the original
- t=y;y=x=x=t			# in the reverse order
- push x; push y; pop x; pop y	# use stack no temporary
- xchng x y			# hardware instruction in x86

and perhaps many other plausible ones.

The terrible thing about an overspecific presentation is that it blinds one 
to alternatives

And as for your wanting a fully non-imperative version:

swap (x, y) = (y, x)

-- the only option in Haskell, natural/easy in python, painfully
laborious but still possible in C -- pass a struct, return a struct,
unpack on return.

You think that the '=' sign not standing for math equality is ok.

How come?

I aver that you (any competent programmer) have worked out/crystallized/created
a switch in your brain.  

In 'programming' state: x=x+1 is not a problem
In 'math' state : it is

And you have become adroit at flipping from one to the other -- you do it
all the time going from rhs to lhs!!

I too have that switch as do most practised programmers.

We are not talking about us, we are talking about noobs.

So my reverse question to you is how do you inculcate this switch in a noob?



More information about the Python-list mailing list