misleading prefix ++

Tim Chase python.list at tim.thechases.com
Sat May 20 15:09:05 CEST 2006


>>>>>++i
>>
>>and the interpreter replies
>>0
>>
>>Don't you think it is misleading when you expect a variable to
>>increment?
>>
> 
> Terribly. So stop expecting it to increment :)
> 
> Seriously, --i is also valid Python. Both expressions apply two unary 
> operators to a name. Would you have these become illegal, or start to 
> mean increment/decrement? Either change would break years of backwards 
> compatibility.

It looks like Python behaves more consistantly than C/C++/Java :) 
By adding plus/minus signs, you get consistant behavior:

 >>> x = 42
 >>> +x
42
 >>> ++x
42
 >>> +++x
42
 >>> ++++x
42
 >>> # ad infinitum

In C-like languages, you get

x: 42
+x: 42
++x: 43
+++x: invalid lvalue in increment -> compile fails
++++x: 45
+++++x: invalid lvalue in increment -> compile fails
++++++x: 48

(actually, g++ accepted this funky syntax, but gcc choked on it, 
so linguistic sludge is more tolerable in C++ than in C)

Looks like the OP should be over on c.l.c++ griping about the 
inconsistancy of the "unary +" and "unary -" operators ;)

-tkc
(still waiting for my brain to kick in on a Sat. morning...)







More information about the Python-list mailing list