how to get the ordinal number in list

Steven D'Aprano steve at pearwood.info
Mon Aug 11 05:44:03 EDT 2014


On Sun, 10 Aug 2014 22:23:18 -0700, 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

Which is inherently sequential: the semantics of Python guarantee that 
the right-hand side will be evaluated before being bound to the names on 
the left-hand side.


> The same applies generally to all programmers brought up on imperative
> style.
> 
> Yeah there are problems that need to address time -- OSes, network
> protocols, reactive systems like window managers etc
> 
> But the vast majority of problems that a programmer is likely to solve
> dont need time.

Incorrect. Most problems are time dependent in the sense that you have to 
do X before you do Y. "Print the file, then delete it" has a very 
different effect to "delete the file, then print it".

Even functional programming has an implicit sense of time: f(g(x)) 
applies f *after* g, not the other way around. While there are some 
functions where it doesn't matter what order you call them, in general it 
does.

There has been a huge amount of research into writing fully parallel 
code, where the order that code is executed is indeterminate. If we could 
take a general sequential program and parallelize it, we could speed up 
our programs hugely by throwing more cores or CPUs into the hardware. But 
in general we can't -- comparatively few tasks are easily, or at all 
parallelizable. One cannot make an omelet by cooking the eggs first and 
beating them second, or even at the same time. And even when you can 
parallelize a series of tasks, it's 

I think this is why both declarative and functional programming idioms 
will remain niche (although important niches). Most tasks are inherently 
imperative to at least some degree, and often a *great* degree.


> What is wrong is then thinking that all *problems* are sequential rather
> than seeing that some over-specific sequential *solutions* to
> non-sequential problems are ok.
> 
> A mindset exemplified by your hilarious statement: "computers have a
> sense of time"

Of course they do. Apart from a very few experimental asynchronous CPUs, 
computers are all based on CPUs with an internal clock signal which 
synchronizes distinct parts of the circuit with each other.



-- 
Steven



More information about the Python-list mailing list