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

yipeiwu_com6年前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 

相关文章

python BlockingScheduler定时任务及其他方式的实现

本文介绍了python BlockingScheduler定时任务及其他方式的实现,具体如下: #BlockingScheduler定时任务 from apscheduler.sc...

python web框架 django wsgi原理解析

前言 django wsgi python有个自带的wsgi模块 可以写自定义web框架 用wsgi在内部创建socket对象就可以了 自己只写处理函数就可以了 django只是web...

基于pycharm导入模块显示不存在的解决方法

基于pycharm导入模块显示不存在的解决方法

最近,同级或者不同级目录下,导入某个模块,显示不存在,可明明存在,百度找了好多没找到,试了 import sys sys.path.append('/path/to/test') /...

一行python实现树形结构的方法

定义 使用内置的defaultdict 我们可以很容易的定义一个树形数据结构 def tree(): return defaultdict(tree) example: json风...

python将类似json的数据存储到MySQL中的实例

python将类似json的数据存储到MySQL中的实例

由于之前对于爬取下来的数据都是存入MongoDB中,想起来还没有尝试存入MySQL,于是将一篇简单的文章爬取下来,存入MySQL试试 这里用到的python模块是pymysql,因为My...