SV: A little disappointed so far

Duncan Booth duncan at NOSPAMrcp.co.uk
Mon May 19 10:29:59 EDT 2003


"Carsten Gehling" <carsten at gehling.dk> wrote in
news:mailman.1053351273.18047.python-list at python.org: 

> I'm a newcomer to Python too (been working with it for about 2
> months), and what I miss sorely is:
> 
> 1) ++ operator

Python assigments are statements not expressions. This means that in 
general you cannot rebind a variable inside an expression (although 
functions could have side effects). The ++ operator is simply a shorthand 
for an assigment, so the only use for it in Python would be to save 1 
character by allowing you to write:
    i++
instead of:
    i+=1

If Python allowed you to embed assignments inside expressions there might 
be more call for this operator, but on its own it simply duplicates 
existing syntax.

> 
> 2) assignments in test-expressions - eg:
> while i++ < 10:
> or
> if row = cursor.fetchone() != None:

The first of these is often (not always) better written as a for loop in 
Python. These days it is often worthwhile looking at any while loop and 
considering whether to use a for loop and an iterator instead.

The second also looks like it should end up being a for loop:

    for row in cursor:
       ...

> 
> 3) the ternary operator - x = y > z ? k : m (and, no, I do NOT like
> any of the workarounds in the Python Cookbook)

A readable form of conditional expression is planned to be added in Python 
2.4. See http://www.python.org/peps/pep-0308.html for the proposal and the 
various options that were discussed.

Apart from the last of these, I think your only problem is that you aren't 
yet thinking in Python. You are trying to write C code using Python syntax 
and haven't yet picked up on all the idioms that make Python unique.

-- 
Duncan Booth                                             duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?




More information about the Python-list mailing list