標籤:

Python 的 type 和 object 之間是怎麼一種關係?

參考第一的答案,A產生了B,B強大後也知道A是什麼,A和B一樣強大。


以下內容摘自python2.5源碼[object.h]

/* Object and type object interface */

/*
Objects are structures allocated on the heap. Special rules apply to
the use of objects to ensure they are properly garbage-collected.
Objects are never allocated statically or on the stack; they must be
accessed through special macros and functions only. (Type objects are
exceptions to the first rule; the standard types are represented by
statically initialized type objects, although work on type/class unification
for Python 2.2 made it possible to have heap-allocated type objects too).

An object has a reference count that is increased or decreased when a
pointer to the object is copied or deleted; when the reference count
reaches zero there are no references to the object left and it can be
removed from the heap.

An object has a type that determines what it represents and what kind
of data it contains. An objects type is fixed when it is created.
Types themselves are represented as objects; an object contains a
pointer to the corresponding type object. The type itself has a type
pointer pointing to the object representing the type type, which
contains a pointer to itself!).

Objects do not float around in memory; once allocated an object keeps
the same size and address. Objects that must hold variable-size data
can contain pointers to variable-size parts of the object. Not all
objects of the same type have the same size; but the size cannot change
after allocation. (These restrictions are made so a reference to an
object can be simply a pointer -- moving an object would require
updating all the pointers, and changing an objects size would require
moving it if there was another object right next to it.)

Objects are always accessed through pointers of the type PyObject *.
The type PyObject is a structure that only contains the reference count
and the type pointer. The actual memory allocated for an object
contains other data that can only be accessed after casting the pointer
to a pointer to a longer structure type. This longer type must start
with the reference count and type fields; the macro PyObject_HEAD should be
used for this (to accommodate for future changes). The implementation
of a particular object type can cast the object pointer to the proper
type and back.

A standard interface exists for objects that contain an array of items
whose size is determined when the object is allocated.
*/


python新手試著回答一下:

python里的一切皆是對象,但是這個對象可以分為兩大類:

1、可以定義type的對象,即type object

2、不可以定義type的對象,即non-object

一些理解的細節:

---- 任何type object的類型就是& ( The type of any type object is )

----如果一個對象是&的實例,那麼他就是type object,否則不是一個type object(If an object is an instance of , then it is a type. Otherwise, it is not a type.

----每個對象都有父類(object除外),即是其他類的子類

----每個對象都是一個實例(type是自身的實例)

----創造對象的方法:1、通過繼承創造對象 2、通過實例化創造對象

----一切皆是實例化。1、通過實例化創造對象,這不需要解釋。2、通過繼承創造對象,本質上是用基類的type object來創建對象。

  1. Reference:http://www.cafepy.com/article/python_types_and_objects/python_types_and_objects.pdf


讀了1樓分享的原文後總結了一下:(基本和葉雨客差的不多)

type objects/類和元類:可實例化和繼承

non-type objects/實例:不能實例化也不能繼承

  1. everything is object (either type or non-type)
  2. everything is created using a type object or metaclass so that its.__class__ == thattype
  3. type objects(classes-codes) are created by type(the class statement )
  4. non-type objects(instances-data) are created and run by type object(the call operator() )
  5. function objects are created by function type object(the def statement )


object是所有類的父類(除了它自己),type(或者type的子類)是所有類的類。


其實很好理解

首先,既然Python裡面一切皆對象(object),那麼有三個事情一定成立:1. 所有的實例(instance)都有對應的類(class);2. 所有的東西(包括函數,λ,甚至代碼本身)都是一個實例(object);3. 所有的class都是object的子類。

從而,object本身就是一個實例,它對應的class是啥呢?就是type

那麼,從上一條可知type是一個class,當然也是一個實例了。它的class是啥?還是type!它的父類是啥?當然是object!它生成的實例是啥?是object!

代碼如下:

Python 2.6.6 (r266:84292, Jan 22 2014, 09:42:36)

In [1]: issubclass(type, object)

Out[1]: True

In [2]: isinstance(object, type)

Out[2]: True

In [3]: isinstance(type, type)

Out[3]: True


不錯,解惑了


學習了


推薦閱讀:

用python爬拉鉤網關於『數據分析』工作的信息為什麼都是空的?
文件里寫的是unicode,用python怎麼讀取成文本?
Sublime Text 3中怎麼更換python的版本?

TAG:Python |

分頁阅读: 1 2