[Python-ideas] Re: while(tt--)

2023-08-21 Thread Wes Turner
On Sat, Aug 19, 2023, 7:27 PM Celelibi  wrote:

> 2023-08-04 8:18 UTC+02:00, daniil.arashkev...@gmail.com
> :
> > Currently in Python we have construction like this:
> >
> > tt = 5
> > while t:
> > # do something
> > tt -= 1
> >
> > It would be great if in Python we have something like this:
> > tt = 5
> > while (tt--):
> > # do something
> >
> > It is exists in C++. And in my opinion it is very Pythonic
>
> Just as a reminder of C/C++: {pre,post}-{inc,dec}rementations bring
> their lot of issues with the language. Mostly the infamous *undefined
> behavors*. If you've done some C or C++ this very expression might
> send shivers down your spine.
>

>From "Increment (++) and decrement (--) operators should not be used in a
method call or mixed with other operators in an expression"
https://rules.sonarsource.com/cpp/RSPEC-881/ :

***

### Noncompliant code example

u8a = ++u8b + u8c--;
foo = bar++ / 4;

### Compliant solution
The following sequence is clearer and therefore safer:

++u8b;
u8a = u8b + u8c;
u8c--;
foo = bar / 4;
bar++;

## Resources
- MISRA C:2004, 12.1 - Limited dependence should be placed on the C
operator precedence rules in expressions.
- MISRA C:2004, 12.13 - The increment (++) and decrement (--) operators
should not be mixed with other operators in an expression.
- MISRA C++:2008, 5-2-10 - The increment (++) and decrement (--) operator
should not be mixed with other operators in an expression.
- MISRA C:2012, 12.1 - The precedence of operators within expressions
should be made explicit
- MISRA C:2012, 13.3 - A full expression containing an increment (++) or
decrement (--) operator should have no other potential side effects other
than that cause by the increment or decrement operator
- CERT, EXP30-C. - Do not depend on the order of evaluation for side effects
- CERT, EXP50-CPP. - Do not depend on the order of evaluation for side
effects
- CERT, EXP05-J. - Do not follow a write by a subsequent write or read of
the same object within an expression

***

https://awesome-safety-critical.readthedocs.io/en/latest/ links to e.g.
MISRA, CERT
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/BBMJQRQYK4AXGIYZ7BAWEWBUTNOOR37H/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: while(tt--)

2023-08-21 Thread Chris Angelico
On Tue, 22 Aug 2023 at 05:34, Wes Turner  wrote:
>
> On Sat, Aug 19, 2023, 7:27 PM Celelibi  wrote:
>>
>> 2023-08-04 8:18 UTC+02:00, daniil.arashkev...@gmail.com
>> :
>> > Currently in Python we have construction like this:
>> >
>> > tt = 5
>> > while t:
>> > # do something
>> > tt -= 1
>> >
>> > It would be great if in Python we have something like this:
>> > tt = 5
>> > while (tt--):
>> > # do something
>> >
>> > It is exists in C++. And in my opinion it is very Pythonic
>>
>> Just as a reminder of C/C++: {pre,post}-{inc,dec}rementations bring
>> their lot of issues with the language. Mostly the infamous *undefined
>> behavors*. If you've done some C or C++ this very expression might
>> send shivers down your spine.
>
>
> From "Increment (++) and decrement (--) operators should not be used in a 
> method call or mixed with other operators in an expression"
> https://rules.sonarsource.com/cpp/RSPEC-881/ :
>

We already know that C programmers are unnecessarily scared of a
construct that, in Python, would not be scary.

Anyway, rules like that have nothing to do with undefined behaviour,
they could just violate a style guide. For example,
https://rules.sonarsource.com/cpp/RSPEC-1774/ is a completely
arbitrary style rule. And
https://rules.sonarsource.com/cpp/RSPEC-1712/ enforces duplicate
functions rather than using argument defaults. And
https://rules.sonarsource.com/cpp/RSPEC-1142/ and, excuse me,
https://rules.sonarsource.com/cpp/RSPEC-909/ ?? Yeah, I don't think
this is anything more than a style guide.

ChrisA
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/VBNDQIHB7ESMQUUHNUDMZFTGM4SDNFW2/
Code of Conduct: http://python.org/psf/codeofconduct/