A Bug By Any Other Name ...

Daniel Fetchinson fetchinson at googlemail.com
Mon Jul 6 22:48:39 EDT 2009


>>>> I wonder how many people have been tripped up by the fact that
>>>>
>>>>     ++n
>>>>
>>>> and
>>>>
>>>>     --n
>>>>
>>>> fail silently for numeric-valued n.
>>>
>>> What do you mean, "fail silently"? They do exactly what you should
>>> expect:
>>>>>> ++5  # positive of a positive number is positive
>>>
>>> I'm not sure what "bug" you're seeing. Perhaps it's your expectations
>>> that are buggy, not Python.
>>
>> Well, those expectations are taken seriously when new features are
>> introduced into the language - and sometimes the feature is dismissed
>> just because it would be confusing for some. If a += 1 works, expecting
>> ++a to have the same meaning is very reasonable (for those coming from
>> languages with a ++ operator, like C or Java) - more when ++a is a
>> perfectly valid expression. If this issue isn't listed under the various
>> "Python gotchas" articles, it should...
>
> The fact that it isn't suggests strongly to me that it isn't that common
> a surprise even for Java and C programmers. This is the first time I've
> seen anyone raise it as an issue.
>
> There are plenty of other languages other than Java and C. If we start
> listing every feature of Python that's different from some other
> language, we'll never end.

Yes, there are plenty of languages other than Java and C, but the
influence of C is admittedly huge in Python. Why do you think loops
are called "for", conditionals "if" or "while", functions return via
"return", loops terminate via "break" and keep going via "continue"
and why is comparison written as "==", etc, etc? All of these are
coming from C (or an even earlier language) and my point is that users
are most of time correct when they assume that something will work the
same way as in C.

So I'd think that putting a warning in a FAQ or a Python Gotchas list
about ++n would be very useful for many users. And it does not imply
that the differences from every other language should be documented in
a similar fashion. Only C :)

Cheers,
Daniel

> For what it's worth, Ruby appears to behave the same as Python:
>
> $ irb
> irb(main):001:0> n = 5
> => 5
> irb(main):002:0> ++n
> => 5
> irb(main):003:0> --n
> => 5
> irb(main):004:0> -+n
> => -5
>


-- 
Psss, psss, put it down! - http://www.cafepress.com/putitdown



More information about the Python-list mailing list