Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1753371
  • 博文数量: 335
  • 博客积分: 4690
  • 博客等级: 上校
  • 技术积分: 4341
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-08 21:38
个人简介

无聊之人--除了技术,还是技术,你懂得

文章分类

全部博文(335)

文章存档

2016年(29)

2015年(18)

2014年(7)

2013年(86)

2012年(90)

2011年(105)

分类: Python/Ruby

2013-01-22 00:11:10


Every object has an identity, a type and a value. An object’s identity never changes once it has been created; you may think of it as the object’s address in memory. The ‘‘ operator compares the identity of two objects; the  function returns an integer representing its identity (currently implemented as its address). An object’s type is also unchangeable.  An object’s type determines the operations that the object supports (e.g., “does it have a length?”) and also defines the possible values for objects of that type. The  function returns an object’s type (which is an object itself). The value of some objects can change. Objects whose value can change are said to be mutable; objects whose value is unchangeable once they are created are called immutable. (The value of an immutable container object that contains a reference to a mutable object can change when the latter’s value is changed; however the container is still considered immutable, because the collection of objects it contains cannot be changed. So, immutability is not strictly the same as having an unchangeable value, it is more subtle.) An object’s mutability is determined by its type; for instance, numbers, strings and tuples are immutable, while dictionaries and lists are mutable.

Objects are never explicitly destroyed; however, when they become unreachable they may be garbage-collected. An implementation is allowed to postpone garbage collection or omit it altogether — it is a matter of implementation quality how garbage collection is implemented, as long as no objects are collected that are still reachable


python是纯血统的面向对象的变成语言,与java不同。

为此我们可以看一下,我们知道java中int为基本数据类型,在持久化的时候,需要包装成Integer类对象。

但是在python中,一切皆对象。什么都是对象,包括你的代码。

为此我们进行一番探究。

# this is just begining
i=5
print hex(id(i))
print type(i)
i=10
print hex(id(i))
print type(i)
print  i.__add__(5) 
然后运行程序发现:

0x125a8c8

0x125a88c

15

通过阅读上面的英文部分,不难发现 

i的 id,value,以及type,所有对象都具有上述三种属性

然后查看帮助文档    

help> id
Help on built-in function id in module __builtin__:
id(...)
    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.)

该函数返回的对象的内存地址值,通过该函数的声明不难发现i也是对象。

细心的读者可能会发现,此处发生了内存泄露,就i在指向10以后,对象5就不在被任何对象所引用。

不用担心,python的开发者早就想好处理方法,即对象的垃圾回收机制。

我们继续讨论int class 的详细情况

help> int
Help on class int in module __builtin__:
class int(object)
 |  int(x[, base]) -> integer
 |
 |  Convert a string or number to an integer, if possible.  A floating point
 |  argument will be truncated towards zero (this does not include a string
 |  representation of a floating point number!)  When converting a string, use
 |  the optional base.  It is an error to supply a base when converting a
 |  non-string.  If base is zero, the proper base is guessed based on the
 |  string content.  If the argument is outside the integer range a
 |  long object will be returned instead.
 |
 |  Methods defined here:
 |
 |  __abs__(...)
 |      x.__abs__() <==> abs(x)
 |
 |  __add__(...)
 |      x.__add__(y) <==> x+y
 |
 |  __and__(...)

 |      x.__and__(y) <==> x&y

此处省略了很多~~~~~~~~~~~~~~~~~~~··

通过帮助文档,我们知道int class 继承自object类,然后看到的该类的相关函数,

从该类的构造函数我们可以知道很多,

rint int(100)

print int('100',8)
# notice here ,something like typedef 
integer=int 
print type(integer)
print hex(id(integer)
# a new object
print type(integer('100',8))

输出内容如下:

100
64

0x1e1f35e0

需要注意的是:integer=int

该语句的作用相当于创建了int class的一个alias或是别名,这样你就可以用它来创建新的对象,很神奇吧

注意此时integer的值为int,id=0x1e1f35e0,type为type类型

下面顺藤摸瓜,自然想知道object类的定义是什么呢?

>>> help(object)
Help on class object in module __builtin__:
class object
 |  The most base type

从帮助文档我们仅能够推测数该class为基类,其它的信息就只能参阅官方文档了。搜索相关资料,发现

Python源码剖析的讲解还是比较有意思的,正在阅读中,后续会补充上~~~~~~~·····

同时也可以发现python的文档的功能还是比较强悍的,要从分的利用好









阅读(4796) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~