Python source code learning: Object creation and object behavior
Source: Internet
Author: User
Before you create and act on an object, let's take a look at the type object. python is a weak type language, but does not mean that python has no type, in python, there is a special object to process the object type. We call it a type object. if we do not know the object type, we cannot open up memory space for the object, because the memory usage is the object metadata and basic object information, which is closely related to the object type, it must appear in p... before you create and act on an object, let's take a look at the type object. python is a weak type language, but does not mean that python has no type, in python, there is a special object to process the object type. We call it a type object. if we do not know the object type, we cannot open up memory space for the object, because the memory usage is the object metadata and basic object information, which is closely related to the object type, it must appear in the type object corresponding to the python object, open the include folder object in the python source code. H File, view the PyTypeObject source code, in about 324th lines:
Typedef struct _ typeobject {
PyObject_VAR_HEAD
Const char * tp_name;/* For printing, in format" . "*/
Py_ssize_t tp_basicsize, tp_itemsize;/* For allocation */
/* Type attribute cache version tag. Added in version 2.6 */
Unsigned int tp_version_tag;
# Ifdef COUNT_ALLOCS
/* These must be last and never explicitly initialized */
Py_ssize_t tp_allocs;
Py_ssize_t tp_frees;
Py_ssize_t tp_maxalloc;
Struct _ typeobject * tp_prev;
Struct _ typeobject * tp_next;
# Endif
} PyTypeObject;
The code above is very long. a struct contains more than 100 lines, but the information contained is mainly divided into the following four categories:
1. the type name, tp_name, is mainly used internally in python and during debugging to identify the type of the object;
2. tp_basicsize and tp_itemsize are used to create information about the size of memory space allocated to these objects;
3. operation information associated with this type of object, such as tp_base pointer to function;
4. type information of the type object.
Key 1. object creation:
Python creates objects in two ways: Python c api and PyInt_Type.
Python C APIs allow users to interact with Python from the C environment. There are two types of APIs: AOL (Abstract Object Layer), that is, generic APIs, the other is COL (Concrete Object Layer), which is an API of the type. AOL has the form of PyObject _ *** and can be applied to any Python Object. the expression is generally expressed: pyObject * jsonbj = PyObject_new (PyObject, & PyInt_Type). The col api is generally as follows: PyObject * jsonbj = PyInt_FromLong (1000); we create a 1000 integer object.
No matter which Python c api is used, Python directly allocates memory because it is a built-in Python object. if we define a class such as class MySelf (object ), for the type MySelf, Python does not use APIs to create the instance object, but Python creates the instance object through the type object corresponding to MySelf. the type object of MySelf is object, all Python will create an instantiated object of MySelf through the object. Run the following code:
Class A (object ):
Pass
A = ()
Type ()
A. _ base __
The result is as follows:
In fact, Python first instantiates the object to run the object construction method, and then instantiates A, which is closely related to the underlying implementation of Python. Any custom Python class will eventually have a common parent class object. the object class will be instantiated at first, once down, until the custom class is instantiated.
Screen Shot at 12.01.15
Object behavior:
There are three very important operation families in object operation information: tp_as_number, tp_as_sequence, and tp_as_mapping. they point to PyNumberMethods, PySequenceMethods, and PyMappingMethods function families respectively. For an object, you can define all the operations in the three function families at the same time, that is, the object can show the characteristics of the numeric object and the characteristics of the associated object. the code is as follows:
Class MyInt (int ):
Def _ getitem _ (self, key ):
Return key + str (self)
A = MyInt (1)
B = MyInt (2)
Print a + B
Print a ['key']
The running result is:
1
2
3
Key1
Finally, let's talk about the type of the type object. The object type is also an object. what is the object type? First, you can determine that it is also an object. We call it a type. This is very, and very much important. it is the PyType_Type struct in the source code. this is in the typeobject. c file of the objects folder. the source code is as follows:
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service