Python learning path 8 - Python object 2

Posted by dokueki@gmail.com on Tue, 04 Jan 2022 21:12:05 +0100

1. Standard operators

1.1 object value comparison

Comparison operators are used if objects of the same type are equal. All built-in types are supported in comparison operations and return Boolean comparison operation values True or False.

<span style="font-size:14px;"><span style="font-size:14px;">>>> 2 == 2
True
>>> 2.33 < 2.44
True
>>> 'abc' == 'xyz'
False
>>> 'abc' < 'xyz'
True
>>> [3, 'abc'] == [3, 'abc']
True
>>> [3, 'abc'] == ['abc', 3]
False
>>> 3 < 4 < 7       #same as (3 < 4) and (4 < 7)
True
>>> 4 > 3 == 3      #same as (4 > 3) and (3 == 3)
True</span></span>

python Comparison operator: < > < = > = =! = < > (< > equals! =, which may not be supported in the future)

1.2 object identity comparison

Python supports not only the comparison of object values, but also the comparison of objects themselves.

Standard type object identity comparison operator

Operator

function

obj1 is obj2

obj1 and obj2 are the same object

obj1 is not obj2

obj1 and obj2 are not the same pair

<span style="font-size:14px;"><span style="font-size:14px;">>>> a = [5, 'hat', -9.3]
>>> b = a
>>> a is b
True
>>> a is not b
False
>>> b = 2.5e-5
>>> b
2.5e-05
>>> a
[5, 'hat', -9.3]
>>> a is b
False
>>> a is not b
True</span></span>

Look at the following example, then the problem comes

<span style="font-size:14px;">>>> a = 1
>>> b = 1
>>> a is b
True
>>> x = 1.2
>>> y = 1.2
>>> x is y
False
>>> </span>

Why do a and b point to the same object? As we all know, when assigning a variable, the Python interpreter will create a new object and assign the reference of its object to the variable. In that case, a. b should point to different objects. Please continue to look at x and Y. X and y do point to different objects, which is in line with our expected results. why?

It turns out that integer objects and strings are immutable objects, and all Python will cache them very efficiently, which will create the illusion that Python does not create new objects when we think Python should create new objects. Python only caches simple integers, and the range of integers cached by Python will change, so do not use this feature. (I don't know how to use it)

1.3 boolean type

Boolean operators are and, or, and not. It is equivalent to & &, |, in c. And!

.

not has the highest priority, followed by and and or.

2 standard type built-in function

Python provides built-in functions for these basic object types: cmp(), repr(), str(), type().

type():

Usage: type(object)

type() takes an object as a parameter and returns its type.

Its return value is an object of type.

<span style="font-size:14px;">>>> type('helloWorld')
<type 'str'>
>>> type(2.0)
<type 'float'>
>>> type(type(4))
<type 'type'>
>>> </span>

cmp():

Usage: cmp(obj1,obj2), assuming that obj1 is less than obj2. It returns a negative integer and a positive integer if obj1 is greater than obj2. Returns 0 if equal

<span style="font-size:14px;">>>> a, b = 4, -12
>>> cmp(a, b)
1
>>> cmp(b,a)
-1
>>> b = -4
>>> cmp(a,b)
1
>>> a, b = 'abc', 'xyz'
>>> cmp(a, b)
-1
>>> cmp(b, a)
1
>>> b = 'abc'
>>> cmp(a, b)
0
>>> </span>

str() and repr()

str() and repr() functions can easily obtain the contents of objects in the form of strings. Type, value, attribute and other information. The string obtained by str() function is readable, while the string obtained by repr() function can usually obtain the object again. In general, obj = eval(repr(obj)) is true. In most cases, the output of the two functions is still the same.

<span style="font-size:14px;"><span style="font-size:14px;">>>> str(1)
'1'
>>> str(2e10)
'20000000000.0'
>>> repr(2e10)
'20000000000.0'
>>> str([0, 5, 5, 9])
'[0, 5, 5, 9]'
>>> repr([0, 5, 5, 9])
'[0, 5, 5, 9]'</span></span>

Publisher: full stack programmer, stack length, please indicate the source for Reprint: https://javaforall.cn/117809.html Original link: https://javaforall.cn