A little disappointed so far

Daniel Fackrell unlearned at DELETETHIS.learn2think.org
Mon May 19 13:10:41 EDT 2003


"Graham Nicholls" <graham at rockcons.co.uk> wrote in message
news:Mv2ya.4193$573.1871 at news-binary.blueyonder.co.uk...
> BTW, why no ++ operator?


The ++ operator has many pitfalls, IMO.

One is the ability to change a variable in an expression (no explicit
assignment).  This leads to greater quantities of side-effects, which make
code more difficult to read (I'd much rather read, e.g.:

for x in y:
   #do something with x

than

for (i=0; i < lengthOfY; i++) { // Is that the right order, or were the last
                                // two the other way around?  I could look
it up,
                                // but I don't have a reason to use it, atm.
        // do something with x[i]
}

Not to mention that the latter will only handle arrays in the C sense, and
extra coding must be done to handle other types of sequences.  With python,
y can be any iterable, and you can define new iterables if you like.  I
thought that this would probably be a good example, as looping constructs
are the primary use case for ++.)

Another issue is that in some cases (which are avoided for this reason), the
order of execution is not fully defined, and can change depending on the
compiler.  Consider:

----
#include <stdio.h>
int main() {
    int x;
    x = 0;
    x = ++x + ++x + x++ + x++; // This is legal C code, but what does it do?
    printf("x is %d", x);
    return 0;
}
----
gcc under Cygwin/Linux: "x is 10"
cc on HPUX: "x is 8"
gcc under HP/UX: "x is 3"

Just as a note, there were no warnings or errors while compiling.

--
Daniel Fackrell (newsgroups.NOSPAM at dfackrell.mailshell.com)
When we attempt the impossible, we can experience true growth.






More information about the Python-list mailing list