increment/decrement operators

BartC bc at freeuk.com
Sat Dec 5 12:18:35 EST 2015


On 05/12/2015 15:43, Terry Reedy wrote:
> On 12/5/2015 9:41 AM, D'Arcy J.M. Cain wrote:
>> On Sat, 5 Dec 2015 13:56:47 +0100
>> Robin Koch <robin.koch at t-online.de> wrote:
>>> x += y works. (Well, it should.)
>>
>> It does, even on objects other than numbers.
>>
>>>>> x = "abc"
>>>>> y = "def"
>>>>> x += y
>>>>> x
>> 'abcdef'
>>
>>> x++ doesn't.
>>
>> No but it's just a special case of the above.
>>
>>>>> x = 1
>>>>> x += 1
>>>>> x
>> 2
>
> Apple is removing the ++ and -- pre- and post- increment and decrement
> operators from Swift 3.0 as redundant with += 1.
> https://github.com/apple/swift-evolution/blob/master/proposals/0004-remove-pre-post-inc-decrement.md
>
>
> The following section is a good summary of why they were never added to
> Python, and should not be.
>
> '''
> Disadvantages of These Operators
>
> 1. These operators increase the burden to learn Swift as a first
> programming language - or any other case where you don't already know
> these operators from a different language.
>
> 2. Their expressive advantage is minimal - x++ is not much shorter than
> x += 1.

The latter is not the same. Some of the differences are:

* ++ and -- are often inside inside expressions and return values 
(unlike x+=1 in Python)

* x++ and x-- return the /current/ value of x, unlike x+=1 even if it 
were to return a value; it would be the new value

* x+=1 requires you to hard-code the value 1, but ++ is not necessarily 
stepping by 1. You can imagine ++ stepping something to its next value.

However, if ++ and -- are only used as statements, then why not simply 
map them to x+=1? In Python, that would need to be x++ and x-- as ++x or 
--x have existing meanings.

-- 
Bartc



More information about the Python-list mailing list