Home  >  Article  >  Backend Development  >  python operator - identity operator for objects (example analysis)

python operator - identity operator for objects (example analysis)

乌拉乌拉~
乌拉乌拉~Original
2018-08-13 17:32:252666browse

Today in this article we will talk about the python identity operator. I hope this article can help you reading.

Identity operator: The identity operator is used to compare the storage units of two objects: python operator - identity operator for objects (example analysis)

Note: The id() function is used to obtain Object memory address.

The following examples demonstrate the operations of all identity operators in Python:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
 
a = 20
b = 20
 
if ( a is b ):
   print "1 - a 和 b 有相同的标识"
else:
   print "1 - a 和 b 没有相同的标识"
 
if ( a is not b ):
   print "2 - a 和 b 没有相同的标识"
else:
   print "2 - a 和 b 有相同的标识"
 
# 修改变量 b 的值
b = 30
if ( a is b ):
   print "3 - a 和 b 有相同的标识"
else:
   print "3 - a 和 b 没有相同的标识"
 
if ( a is not b ):
   print "4 - a 和 b 没有相同的标识"
else:
   print "4 - a 和 b 有相同的标识"

The output results of the above examples are as follows:

1 - a 和 b 有相同的标识
2 - a 和 b 有相同的标识
3 - a 和 b 没有相同的标识
4 - a 和 b 没有相同的标识

The above are the identity operations in Python I hope this article will be helpful to you as you read it.

The above is the detailed content of python operator - identity operator for objects (example analysis). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn