Python --- is和is not (身份运算符) 规律总结

在整理笔记的时候看到自己做出的结果与老师上课讲的不同,在网上找到的回答也是众说纷纭,但最后发现他们的说法都是对的,但都不算全

基础知识:

对于任意变量都有 id type 和value 三个基本元素

is 和 is not 是判断两个变量 id是否相同的运算符

id(身份标识符)对应着该变量在内存的地址 也是判断变量是否为同一对象的唯一标识符

id相同->共用同一内存地址, 为同一对象

id不同->内存地址不同,不是同一对象

测试开始:

#执行的代码
a = 1
b = 1
c = [1, 2, 3]
d = [1, 2, 3]
e = "hello"
f = 'hello'
g = (1, 2, 3)
h = (1, 2, 3)
i=1.2
j=1.2
k={'a':1}
l={'a':1}
m={1,2,3}
n={1,2,3}
print("int类型:",a is b)
print("float类型:",i is j)
print("str类型:",e is f)
print("列表类型:",c is d)
print("元组类型:",g is h)
print("字典类型:",k is l)
print("集合类型:",m is n)

(出现问题的代码) 

#PyCharm运行结果
int类型: True
float类型: True
str类型: True
列表类型: False
元组类型: True
字典类型: False
集合类型: False

#Jupyter Notebook 运行结果
int类型: True
float类型: False
str类型: True
列表类型: False
元组类型: False
字典类型: False
集合类型: False

(两种软件不同的运行结果)

#修改后代码
a = 1024000
b = 1024000
c = [1, 2, 3]
d = [1, 2, 3]
e = "Life is too short to spend time with people who suck the happiness out of you. If someone wants you in their life, they’ll make room for you. You shouldn’t have to fight for a spot. Never, ever insist yourself to someone who continuously overlooks your worth. And remember, it’s not the people that stand by your side when you’re at your best, but the ones who stand beside you when you’re at your worst that are your true friends"
f = 'Life is too short to spend time with people who suck the happiness out of you. If someone wants you in their life, they’ll make room for you. You shouldn’t have to fight for a spot. Never, ever insist yourself to someone who continuously overlooks your worth. And remember, it’s not the people that stand by your side when you’re at your best, but the ones who stand beside you when you’re at your worst that are your true friends'
g = (1, 2, 3)
h = (1, 2, 3)
i=1.2
j=1.2
k={'a':1}
l={'a':1}
m={1,2,3}
n={1,2,3}
print("int类型:",a is b)
print("float类型:",i is j)
print("str类型:",e is f)
print("列表类型:",c is d)
print("元组类型:",g is h)
print("字典类型:",k is l)
print("集合类型:",m is n)

(修改后的代码)

#PyCharm 运行结果
int类型: True
float类型: True
str类型: True
列表类型: False
元组类型: True
字典类型: False
集合类型: False

#Jupyter Notebook 运行结果
int类型: False
float类型: False
str类型: False
列表类型: False
元组类型: False
字典类型: False
集合类型: False

#命令行执行该文件 结果
int类型: True
float类型: True
str类型: True
列表类型: False
元组类型: True
字典类型: False
集合类型: False

#命令行 交互式执行代码 结果
int类型: False
float类型: False
str类型: False
列表类型: False
元组类型: False
字典类型: False
集合类型: False

(各种方式的执行结果)

总结:

可以看出不同的编写方式有着不同的结果(原因是个人感觉,规律是客观总结):

  • 交互式编程(如在命令行一条一条的输入或者在Jupyter Notebook上):
  1. 整型和字符串类型的内存在开辟时会根据其大小来判断是否需要开辟新空间,较小的共用同一地址(即id相同),较大的开辟新地址(id不同);
  2. 剩下的其他类型无论大小,一律开辟新地址

  • 脚本式编程(如在命令行内直接执行py文件或者在PyCharm上):
  1. 不可变对象(整型,浮点型,字符串,元组),无论大小,都不会开辟新内存
  2. 可变对象(剩下的那几种),无论大小,都会开辟新内存

具体实现过程以我的能力还回答不上来,能想到的是交互式编程每一条语句编译一次,所以满足了一定条件就会给这个变量开辟空间,脚本式编程一次编译一个文件,所以会进行一些其他处理,提高编译速度,不要这样一个一个的判断是否开辟空间(当然这个解释不一定是对的)

  • 13
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Star※

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值