Python的object和type理解及主要对象层次结构

1、Object与Type

一、摘自Python Documentation 3.5.2的解释html

Objects are Python’s abstraction for data. All data in a Python program is represented by objects or by relations between objects. (In a sense, and in conformance to Von Neumann’s model of a “stored program computer,” code is also represented by objects.)ide

对象是Python对数据的抽象。Python程序中的全部数据都由对象或对象之间的关系表示。(在某种意义上,而且符合冯·诺依曼的“存储程序计算机”的模型,代码也由对象表示的)。函数

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 ‘is‘ operator compares the identity of two objects; the id() function returns an integer representing its identity.spa

每一个对象都有一个标识,一个类型和一个值。对象的身份一旦建立就不会改变; 你能够把它看做内存中的对象地址。is运算符比较两个对象的标识; id()函数返回一个表示其身份的整数。设计

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 type() function returns an object’s type (which is an object itself). Like its identity, an object’s type is also unchangeable.code

对象的类型决定对象支持的操做(例如,“它有长度吗?”),而且还定义该类型对象的可能值。type()函数返回一个对象的类型(它是一个对象自己)。与它的身份同样,对象的类型也是不可改变的。orm

二、Pyhtml的解释:htm

object:对象

class object
      The most base type

type:blog

class type(object)
      type(object_or_name, bases, dict)
      type(object) -> the object's type
      type(name, bases, dict) -> a new type

 

从上图能够看出,对象obeject是最基本的类型type,它是一个总体性的对数据的抽象概念。相对于对象object而言,类型type是一个稍微具体的抽象概念,说它具体,是由于它已经有从对象object细化出更具体抽象概念的因子,这就是为何type(int)、type(float)、type(str)、type(list)、type(tuple)、type(set)等等的类型都是type,这也是为何instance(type, object)和instance(object, type)都为True的缘由,即类型type是做为object、int、float等类型的总体概念而言的。那么,为何issubclass(type, object)为True,而issubclass(object, type)为Flase呢?从第二张图,即从继承关系能够看到,type是object的子类,所以前者为True,后者为False。从Python语言的总体设计来看,是先有对象,后有具体的类型,即对象产生类型,类型从属于对象,二者是相互依存的关系。因此:

  • object是Python对数据的抽象,它是Python程序对数据的集中体现。
  • 每一个对象都有一个标识,一个类型和一个值。
  • 对象的类型决定对象支持的操做。
  • 某些对象的值能够更改。 其值能够改变的对象称为可变对象;对象的值在建立后不可更改的对象称为不可变对象。

 

2、对象层次结构

相关文章
相关标签/搜索