举例讲解Python中的身份运算符的使用方法

yipeiwu_com5年前Python基础

Python身份运算符
身份运算符用于比较两个对象的存储单元

以下实例演示了Python所有身份运算符的操作:

#!/usr/bin/python

a = 20
b = 20

if ( a is b ):
  print "Line 1 - a and b have same identity"
else:
  print "Line 1 - a and b do not have same identity"

if ( id(a) == id(b) ):
  print "Line 2 - a and b have same identity"
else:
  print "Line 2 - a and b do not have same identity"

b = 30
if ( a is b ):
  print "Line 3 - a and b have same identity"
else:
  print "Line 3 - a and b do not have same identity"

if ( a is not b ):
  print "Line 4 - a and b do not have same identity"
else:
  print "Line 4 - a and b have same identity"

以上实例输出结果:

Line 1 - a and b have same identity
Line 2 - a and b have same identity
Line 3 - a and b do not have same identity
Line 4 - a and b do not have same identity 

相关文章

django的分页器Paginator 从django中导入类

django的分页器Paginator 从django中导入类

先创建表,然后生成批量数据。 在models文件里 from django.db import models # Create your models here. class...

python用来获得图片exif信息的库实例分析

本文实例讲述了python用来获得图片exif信息的库用法。分享给大家供大家参考。具体分析如下: exif-py是一个纯python实现的获取图片元数据的python库,官方下载地址:...

python2.6.6如何升级到python2.7.14

python2.6.6如何升级到python2.7.14

其实网上有很多关于python2.6.6 升级到python2.7的文章,但是我对比这些类似的文章升级之后,发现其中有错误的地方,于是决定还是自己写一个真正的升级过程。 我的虚拟机里安装...

用map函数来完成Python并行任务的简单示例

用map函数来完成Python并行任务的简单示例

众所周知,Python的并行处理能力很不理想。我认为如果不考虑线程和GIL的标准参数(它们大多是合法的),其原因不是因为技术不到位,而是我们的使用方法不恰当。大多数关于Python线程和...

教你如何在Django 1.6中正确使用 Signal

简单回答是: 在其他方法无法使用的情况下, 才最后考虑使用signal. 因为新的django开发人员得知signal之后, 往往会很高兴去使用它. 他们在能使用signal的地方就使用...