python 底層實現原理

python framework

在這裏插入圖片描述

//object base class
typedef struct _object {
 PyObject_HEAD
} PyObject;

//variable length object base class
typedef struct {
 PyObject_VAR_HEAD
} PyVarObject;

PyObject_VAR_HEAD中int ob_refcnt

[object.h]
#ifdef Py_TRACE_REFS
/* Define pointers to support a doubly-linked list of all live heap
objects. */
#define _PyObject_HEAD_EXTRA \
 struct _object *_ob_next;  \
 struct _object *_ob_prev;
#define _PyObject_EXTRA_INIT 0, 0,
#else
#define _PyObject_HEAD_EXTRA
#define _PyObject_EXTRA_INIT
#endif
/* PyObject_HEAD defines the initial segment of every PyObject. */
#define PyObject_HEAD \
 _PyObject_HEAD_EXTRA \
 int ob_refcnt;  \       //引用計數
 struct _typeobject *ob_type;  //類型對象
#define PyObject_VAR_HEAD \
 PyObject_HEAD \
 int ob_size; /* Number of items in variable part */

python 中對象的分類:

  • math: 數值對象,int, float, long
  • container:容納其他對象的集合對象, list, set, dict
  • composition:表示程序結構的對象, class,function
  • internal:運行時內部使用的對象
    在這裏插入圖片描述

引用計數(reference count)

類型對象不受引用計數限制,永遠不會被析構(de-constructor)

PyIntObject 對象

immutable 對象

PyObject *PyInt_FromLong(long ival)
PyObject* PyInt_FromString(char *s, char **pend, int base)
#ifdef Py_USING_UNICODE
PyObject*PyInt_FromUnicode(Py_UNICODE *s, int length, int base) #endif

PyIntObject 對象是 Immutable 對象

小數對象

小數範圍確定:

NSMALLPOSINTS: upper bound
NSMALLNEGINTS  : lower bound

所有小數對象常駐內存

大數對象

動態使用固定內存,這塊內存通過鏈表的形式進行組織

>>> id(a)
140392026626384
>>> id(b)
140392026626384
>>> b=300
>>> a
100
>>> id(b)
140392025569344

PyStringObject

[stringobject.h]
typedef struct 
{ 
	PyObject_VAR_HEAD long ob_shash;  //cache the string hash value
	int ob_sstate; 	    // intern flag	
	char ob_sval[1];	// the pointer, point to the string memory
} PyStringObject;
[stringobject.c]
PyTypeObject PyString_Type = 
{ 
PyObject_HEAD_INIT(&PyType_Type) 0,
"str",
sizeof(PyStringObject), sizeof(char),
......
(reprfunc)string_repr,
/* tp_repr */

s=“Python” 字符串的內存分佈
在這裏插入圖片描述

字符緩衝池

PyListObject

typedef struct {
PyObject_VAR_HEAD
/* Vector of pointers to list elements. list[0] is ob_item[0],
etc. */
   PyObject **ob_item;
int allocated; } PyListObject;

PyDictObject

Hash 方法:開放定址 法。

typedef struct 
{
	long me_hash; /* cached hash code of me_key */ 
	PyObject *me_key;
	PyObject *me_value; 
} PyDictEntry
#define PyDict_MINSIZE 8
typedef struct _dictobject PyDictObject;
struct _dictobject 
{ 
	PyObject_HEAD
	int ma_fill; /* # Active + # Dummy */ 
	int ma_used; /* # Active */
	int ma_mask;
	PyDictEntry *ma_table;
	PyDictEntry *(*ma_lookup)(PyDictObject *mp, PyObject *key, long 	hash);
	PyDictEntry ma_smalltable[PyDict_MINSIZE]; 
};

Python 編譯和運行

編譯

PyCodeObject

Pyc 文件

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章