0 some built-in functions commonly used in basic python-4.4

Source: Internet
Author: User

1. Compare two objects, we use CMP at 2.x, but at 3.x we use the function inside the operator module


LT (A, b) Equivalent to a < b

Le (A,b) Equivalent to a<=b

eq (A,b) Equivalent to a= =b

NE (A,b) Equivalent to a!=b

GT (A,b) Equivalent to a>b

ge (A, b) equivalent a>=b

Returns TRUE or False

2.int (obj), str (obj), float (obj), etc.

These functions convert the Obj object to the appropriate type, but it is important to note that not all types can be mutually rotating, so let's take an example below:



3.type (obj) and isinstance (OBJ1,OBJ2)

Type (obj) has been said before, which is no longer described in detail


Isinstance (OBJ1,OBJ2) Comparison of two objects

The differences between the two functions are:

Type cannot recognize the parent class, and isinstance can recognize the parent class

Class Foo (object):    passclass Bar (foo):    passprint (Type (foo)) #类型print (Type (Bar)) #类型print (Type (foo ())) # Instance print (Type (bar))) #实例print (object) print (Type (foo)) print (Type (foo) ==object) print (Type (bar) ==foo) print (Type ( Foo ()) ==object) print (Type (foo ()) ==foo) print (Type (Bar ()) ==foo) print (Isinstance (foo (), object)) print (Isinstance ( Bar (), object)) print (Isinstance (bar (), Foo))

Output Result:

>>> ================================ RESTART ================================
>>>
<class ' type ' >
<class ' type ' >
<class ' __main__. Foo ' >
<class ' __main__. Bar ' >
<class ' object ' >
<class ' type ' >
False
False
False
True

-------------Split Line
False
True
True
True

We temporarily ignore the section in front of the dividing line and focus on the part behind the split line, and you can see that the type cannot recognize the parent class, and isinstance can recognize the parent class


Right here, thank you.

------------------------------------------------------------------

Click to jump 0 basic python-Catalogue



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

0 some built-in functions commonly used in basic python-4.4

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.