2

I am studying the CPython's objects system implementation and I struggling to understand the differences between and purposes of the PyTypeObject and PyType_Type structs.

At first sight, I thought that PyTypeObject structs would hold the information common to all objects of a certain class. However, PyTypeObject structs have an attributes dictionary (tp_dict) and it would not make sense to have all objects of a certain class share same dicitionary. So I am starting to think that PyTypeObjects are more like molds that are created with dummy values when an object is created and then filled with custom attributes' values later (upon tp_init's call, for example). It seems to me that these molds are created with the information stored in the PyType_Type struct of the objects type.

Even though this second interpretation makes more sense than the first one I thought about, it does not seem like that is what really happens for the implementation of Tuples have only a single struct, all of which have tp_name set to "tuple" and not to "type".

What are the purposes of PyTypeObject structs and PyType_Type structs and where are the attributes of a certain object stored?

1
  • In Python, types are objects too. And because objects use a dict to hold their fields (except for __slots__), I am not surprised that types have a dict like regular instances. And when you define a class in Python, you (usually) provide some methods, which will get bound when an instance is created, but the type itself holds the "raw" methods (that you still can call MyClass.some_method(instance_as_self) works fine. I remember having read how they worked a few years ago, but I don't recall enough to help you without committing the time to read again myself. Commented Nov 28, 2022 at 13:55

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.