為什麼在Python中沒有專門的char數據類型呢?
為什麼在Python中沒有專門的char數據類型
Python 之禪♂第三條
Simple is better than complex.
在 Python 中, string 中的每個字元占的空間大小是 8 bit.
&>&>&> import sys
&>&>&> sys.getsizeof()
37
&>&>&> sys.getsizeof(a)
38
typedef struct {
PyObject_VAR_HEAD
long ob_shash;
int ob_sstate;
char ob_sval[1];
/* Invariants:
* ob_sval contains space for ob_size+1 elements.
* ob_sval[ob_size] == 0.
* ob_shash is the hash of the string or -1 if not computed yet.
* ob_sstate != 0 iff the string object is in stringobject.cs
* interned dictionary; in this case the two references
* from interned to this object are *not counted* in ob_refcnt.
*/
} PyStringObject;
/* This dictionary holds all interned strings. Note that references to
strings in this dictionary are *not* counted in the strings ob_refcnt.
When the interned string reaches a refcnt of 0 the string deallocation
function will delete the reference from this dictionary.
Another way to look at this is that to say that the actual reference
count of a string is: s-&>ob_refcnt + (s-&>ob_sstate?2:0)
*/
static PyObject *interned;
typedef struct {
PyObject_HEAD
long ob_ival;
} PyIntObject;
總而言之, 這樣的設計滿足 python 的 "一切都是對♂象♂", "一切都儘可能simple" 的設計思想.
推薦閱讀:
※python初學者,如果你連這樣的習題寫不出代碼,該怎麼辦?
※Fluent Python 筆記(四):字典和集合
※Python GUI教程(七):轉換qt設計師的ui代碼為Python代碼
※python多進程進程間通信疑問,求大神指教?(主進程獲取不到子進程變數)
※Python程序中不同的重啟機制