There are many differences between Python and C + +, some of which are easily overlooked and cause a program error. Here is a list of some, for everyone's reference. Continuous update ...
Python does not have a self-increment decrement operator
There are ++i, i++ 、--i, I--。 Python does not have these operators. A common loop for C/s + + is:
for (int i = 0; i < ++i) { std::cout << i << Endl;}
and the python corresponding loop is:
For I in range: print (i)
Perhaps this is one of the important reasons that Python does not provide the self-increment decrement operator: there is no such common use case. If you really need to, most of the cases can be replaced with i + = 1,i-=.
Note that although Python does not support the + + operator, ++i does not error. ++i is interpreted as + (+ (i)).
>>>> i = 3>>> ++i3>>> i++syntaxerror:invalid syntax
implicit return of a Python function A common error in C + +: "Not all control paths return a value". If any of the paths do not return a value, an error is found.
Error:not all control Paths Returnsint F (bool flag) { if (flag) { return 1; }}
but the corresponding Python program does not have any problems:
def f (flag): If flag: return 1>>> print (f (True)) >>> 1>>> print (f (False)) >> > None
Although Python does not error, it is recommended that you explicitly return None if the function has a return value. This way, other people will not be confused when they see this code: Does the author mean to rely on an implicit return to return none, or to forgetA value of return?
Update in ...
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
How to distinguish between Python and C + + (update ...) )