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