zoukankan      html  css  js  c++  java
  • Python身份运算符

    Python身份运算符:

      is :判断左右两个对象内存地址是否相等。

      is not :判断左右两个对象内存地址是否不相等。

      注:对于不可变类型数据,当引用自相同数据时,is 返回值为 True 。

          数字、字符串、元组。

        对于可变类型数据,当引用自相同数据时,is not 返回值为 True 。

          列表、字典、集合。

    # 对于不同类型数据,is not 为真
    num = 123
    strs = 'hello'
    
    # 输出 num 和 strs 的地址
    print("num地址为:{0},strs地址为:{1}".format(id(num),id(strs)))
    # num地址为:140729798131792,strs地址为:1692203499952
    print(num is strs)
    # False ,地址不同
    print(num is not strs)
    # True
    
    # 对于不可变类型数据,引用自相同数据时,is 为真
    # 数字
    num = 123
    num_two = 123
    # 输出 num 和 num_two 的地址
    print("num地址为:{0},num_two地址为:{1}".format(id(num),id(num_two)))
    # num地址为:140729798131792,num_two地址为:140729798131792
    print(num is num_two)
    # True ,num 和 num_two 指向同一块内存地址
    print(num is not num_two)
    # False
    
    # 字符串
    strs = 'hello'
    strs_two = 'hello'
    # 输出 strs 和 strs_two 的地址
    print("strs地址为:{0},strs_two地址为:{1}".format(id(strs),id(strs_two)))
    # strs地址为:1248715293104,strs_two地址为:1248715293104
    print(strs is strs_two)
    # True
    print(strs is not strs_two)
    # False
    
    # 元组
    tup = (1,2,3)
    tup_two = (1,2,3)
    # 输出 tup 和 tup_two 的地址
    print("tup地址为:{0},tup_two地址为:{1}".format(id(tup),id(tup_two)))
    # tup地址为:2065334183480,tup_two地址为:2065334183480
    print(tup is tup_two)
    # True
    print(tup is not tup_two)
    # False
    
    
    
    # 对于可变类型,即使引用自相同数据,内存地址也不相同。is not 为 True
    # 列表
    lst = [1,2,3]
    lst_two = [1,2,3]
    # 输出 lst 和 lst_two 的地址
    print("lst地址为:{0},lst_two地址为:{1}".format(id(lst),id(lst_two)))
    # lst地址为:2781811921480,lst_two地址为:2781811921992
    print(lst is lst_two)
    # False
    print(lst is not lst_two)
    # True
    
    # 字典
    dic = {'a':123,'b':456}
    dic_two = {'a':123,'b':456}
    # 输出 dic 和 dic_two 的地址
    print("dic地址为:{0},dic_two地址为:{1}".format(id(dic),id(dic_two)))
    # dic地址为:2871230999272,dic_two地址为:2871230999352
    print(dic is dic_two)
    # False
    print(dic is not dic_two)
    # True
    
    # 集合
    sets = {'a','b'}
    sets_two = {'a','b'}
    # 输出 sets 和 sets_two 的地址
    print("sets地址为:{0},sets_two地址为:{1}".format(id(sets),id(sets_two)))
    # sets地址为:2089152127048,sets_two地址为:2089152127720
    print(sets is sets_two)
    # False
    print(sets is not sets_two)
    # True

    2020-02-05

  • 相关阅读:
    Springboot + Atomikos + Druid + Mysql 实现JTA分布式事务
    JAVA生成一个二维数组,使中间元素不与相邻的9个元素相等,并限制每一个元素的个数
    java.net.UnknownHostException: lc001 未知的网络服务
    Maven 多模块引用版本的问题 java.lang.NoSuchMethodError
    Maven项目运行Junit测试用例 出现ClassNotFound
    CAS5.X 集群配置 初版
    调试CAS源码步骤
    openhtmltopdf 支持自定义字体、粗体
    Java HTML to PDF 支持SVG
    .Net 框架
  • 原文地址:https://www.cnblogs.com/hany-postq473111315/p/12263746.html
Copyright © 2011-2022 走看看