How to distinguish between Python and C + + (update ...) )

Source: Internet
Author: User

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 ...) )

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.