How are the characters and numbers in Python compared to the size?

Source: Internet
Author: User

Reply content:

@ Yan Hang's answer is not right.

Reference 5. Built-in the instructions in Types, the CPython is compared according to the following rules (own summary):

  1. Any two objects can be compared
  2. Objects of the same type (instances), if they are numeric (Int/float/long/complex), are compared by simple size, if they are non-numeric, and __cmp__ (including __gt__,__lt__, etc.) are defined in the class (type), compared by __cmp__. Otherwise, by address (ID) to compare
  3. Different types of objects (instances), if one of the comparison objects is a numeric type (Int/float/long/complex, etc.), then the <其它非数字型的对象;如果两个都是非数字型的对象,则按照类型名的顺序比较,如{}> < "abc"(按照"dict"="" "str"),而"abc"=""> object of the number type [up], "ABC" < (.)
  4. For a custom class (type) instance, if it inherits from the base type, it is compared by the rule of the base type (1-3). Otherwise, Old-style class < New-style class, New-style class is compared by type name order, and Old-style class is compared by address
  5. The bool type is a subclass of int, and true=1, false=0, compare by 1-4, such as true >-1, True < 4.2, true < "abc", etc.
The above answer is a big change for python2.x,3.x, such as when str and int compare, throw an exception.

Back to the topic, the comparison of characters and numbers conforms to rule 3, so the numbers<>
100< ' a '

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