python的万物皆对象以及int深入

来源:互联网 发布:知识更新速度数据 编辑:程序博客网 时间:2024/05/17 02:34

在python语言中,万物皆对象,非常pure。不像java还有primitive data types这些非对象类型存在。


首先介绍一下is id ==这三个东西:

1、id(object) -> integer

    Return the identity of an object.  This is guaranteed to be unique among simultaneously existing objects.  (Hint: it's the object's memory address.)

返回对象的内存地址,对于每个对象其值都不相同,可将其看做中国人的身份证号码。

2、==:调用对象的__cmp__方法进行比较,自定义对象可以重载(即自定义规则)。

3、is : obj1 is obj2与id(obj1)==id(obj2)等效


int

实践发现int类型的[-5, 256]是共享的,唯一的。

代码:

<span style="font-size:18px;">In [77]: for i in range(100,1000):   ....:     a=i+1-1   ....:     if a is not i:   ....:         print(i)   ....:         break   ....:257In [78]: for i in range(-300,1):   ....:     a=i+1-1   ....:     if a is i:   ....:         print(i)   ....:         break   ....:-5</span>


0 0
原创粉丝点击