Now I'm looking at Python3.0 source code.
PyTypeObject PyUnicode_Type = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
"str", /* tp_name */
sizeof(PyUnicodeObject), /* tp_size */
...(snip)...
&unicode_as_number, /* tp_as_number */
&unicode_as_sequence, /* tp_as_sequence */
&unicode_as_mapping, /* tp_as_mapping */
(hashfunc) unicode_hash, /* tp_hash*/
0, /* tp_call*/
(reprfunc) unicode_str, /* tp_str */
...(snip)...
This is a code snippet from Python3.01 unicode object implementation. tp_as_sequence is a behavior definition when the object is used as a sequence, tp_as_mapping is a behavior when used as a mapping object. unicode_as_sequence is defined like this.
static PySequenceMethods unicode_as_sequence = {
(lenfunc) unicode_length, /* sq_length */
PyUnicode_Concat, /* sq_concat */
(ssizeargfunc) unicode_repeat, /* sq_repeat */
(ssizeargfunc) unicode_getitem, /* sq_item */
0, /* sq_slice */
0, /* sq_ass_item */
0, /* sq_ass_slice */
PyUnicode_Contains, /* sq_contains */
};
unicode_repeat etc. are just a simple C function.
Another code snippet taken from bool object implementation. bool is a subtype of int. It is specified by setting PyLong_Type(i.e. int) to tp_base entry.
PyTypeObject PyBool_Type = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
"bool",
...(snip)...
&PyLong_Type, /* tp_base */
...(snip)...
};
Simple enough.
(Above code snippets are Python licensed)
No comments:
Post a Comment