Python build-in function __cmp__

Source: Internet
Author: User

Read the notes of Wang Chunye predecessors, and encountered a very pit case.

A namespace is a mapping from names to objects

>>> Class A:
def __cmp__ (Self,other):
If other > 0:print ' other > 0 '; return-1
Elif Other < 0:print ' other < 0 '; return 1
Else:print ' other==0 '; return 0


>>>
>>>
>>>
>>> O=a ()
>>>
>>> if (o >): print ' TT '

Other > 0
>>> if (o <): print ' TT '

Other > 0
Tt
>>>
>>>
>>>
>>>
>>> if (o > -10): print ' TT '

Other < 0
Tt
>>> if (O < -10): print ' TT '

Other < 0

3 values of return in __cmp__ 1,-1,0

Execution process:

1, according to the value of other to determine the execution of the article, the result is to return 1,-1, or 0

2 to determine if the return value matches the symbol in the expression, the match returns True, and the mismatch returns false

Python build-in function __cmp__

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.