python面向?qū)ο螅ㄒ唬腃語言開始

一切皆為對象。在java中對象是數(shù)據(jù)和方法的結(jié)合,而python中關(guān)于對象的定義是:

凡能當(dāng)作參數(shù)傳遞,就是對象

Python 對象的實(shí)現(xiàn)

由于大部分人使用的是Cpython編譯好的Python程序, 在Cpython中是這樣實(shí)現(xiàn)對象的:

typedef struct _object {
  struct _object *_ob_next;
  struct _obeect *_ob_prev;
  Py_ssize_t ob_refcnt;
  struct _typeobject *ob_type;
}PyObject_VAR_HEAD, PyObject_HEAD

凡是使用了_object 就是對象

一個簡單的鏈表結(jié)構(gòu),加上引用計數(shù)refcnt和類型ob_type就是一個對象的全部。
在python中,定義了int list dict string等基本對象,全都是_object的改寫。以int類型為例子:

typedef struct {
// PyObject_HEAD 是 _object的簡寫
  PyObject_HEAD
  long ob_ival;
} PyIntObject

所以,可以簡單的講: 一切基本對象無非是在_object中增加了某些參數(shù)。

Python對象中的_typeobject

在看對象的定義,其中有一個_typeobject實(shí)現(xiàn)如下:

typedef struct _typeobject {
    PyObject_VAR_HEAD
    const char *tp_name; /* For printing, in format "<module>.<name>" */
    Py_ssize_t tp_basicsize, tp_itemsize; /* For allocation */

    /* Methods to implement standard operations */

    destructor tp_dealloc;
    printfunc tp_print;
    getattrfunc tp_getattr;
    setattrfunc tp_setattr;
    cmpfunc tp_compare;
    reprfunc tp_repr;

    /* Method suites for standard classes */

    PyNumberMethods *tp_as_number;
    PySequenceMethods *tp_as_sequence;
    PyMappingMethods *tp_as_mapping;

    /* More standard operations (here for binary compatibility) */

    hashfunc tp_hash;
    ternaryfunc tp_call;
    reprfunc tp_str;
    getattrofunc tp_getattro;
    setattrofunc tp_setattro;

    /* Functions to access object as input/output buffer */
    PyBufferProcs *tp_as_buffer;

    /* Flags to define presence of optional/expanded features */
    long tp_flags;

    const char *tp_doc; /* Documentation string */

    /* Assigned meaning in release 2.0 */
    /* call function for all accessible objects */
    traverseproc tp_traverse;

    /* delete references to contained objects */
    inquiry tp_clear;

    /* Assigned meaning in release 2.1 */
    /* rich comparisons */
    richcmpfunc tp_richcompare;

    /* weak reference enabler */
    Py_ssize_t tp_weaklistoffset;

    /* Added in release 2.2 */
    /* Iterators */
    getiterfunc tp_iter;
    iternextfunc tp_iternext;

    /* Attribute descriptor and subclassing stuff */
    struct PyMethodDef *tp_methods;
    struct PyMemberDef *tp_members;
    struct PyGetSetDef *tp_getset;
    struct _typeobject *tp_base;
      ..
 }

也就是說: _object的結(jié)構(gòu)體中引入了 _typeobject_typeobject 又指向了_object。這就是一個循環(huán)指針。
可以看到_object通過指向_typeobject獲取了大量的屬性。他們包括:

  • python的基本方法
  • hash操作
  • 類型及相關(guān)方法

簡單的講 ,_object中指向的_typeobject中如果tp_call有值, 那么這個_object就具備函數(shù)的特性,如果tp_as_number有值,那么就具備number類的所有屬性和方法。因此,當(dāng)你在C語言層面為某個_typeobject所有的屬性都設(shè)置一個有效值,那么,你就可以說:

這個對象即是數(shù)字,也是字符串;是函數(shù),也是類; 是一個list,也是一個dict。

哪些不是對象

那些不能當(dāng)作python參數(shù)進(jìn)行傳遞的,例如語法解析(AST)過程中定義的關(guān)鍵詞. 例如:
yield for = is or and等等

這些作為語法解析存在的,并不實(shí)際與_object打交道,只是一個通往_object的橋梁。因此不能視為對象。

并非所有語言都是這樣的實(shí)現(xiàn)方法。java將類和對象 函數(shù)等進(jìn)行了嚴(yán)格區(qū)分,在C語言上并沒有一個通用的_object存在

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容