[Python Basics] Python 2 is different from Python 3, an unknown pit about objects

Source: Internet
Author: User

A pit, don't know why, just record it.

1  while loop! = 0:2     // omit operation 3     print  loop4      if loop = = 0:5         print  (ID (loop))6          print (ID (0))

Running results in Python 2.7

Skip loop printing with loop not 0
03809311234396768

Running results in Python 3.x

Skip loop printing with loop non 0 0495494208495494208

Loop is iterated (not-1) in the search, such as modulo operations

In the process of iterative operation, loop object has been changing, when loop = = 0, in python 2 appears loop = = 0 but the memory address (ID) of the loop is not equal to the ID of 0

At this point the value of loop is printed in int is 0, and is printed with float 0.0

But the ID (loop) is the not ID (0)

This problem was not encountered in Python 3, the same piece of code, after running to loop = = 0 in 3, id (loop) = = ID (0)

For the time being, don't know why, just record it.

[Python Basics] Python 2 is different from Python 3, an unknown pit about objects

Related Article

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.