Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is "type" a keyword

Tags:

python

c

When I read the Python C API Reference Manual, it points to below from https://docs.python.org/3/c-api/type.html#c.PyType_Type

type PyTypeObject

Part of the Limited API (as an opaque struct).

The C structure of the objects used to describe built-in types.

I wonder is "type" from "type PyTypeObject" a keyword of C, I only know there is typedef in C, is it an alias for "struct" in C, I did not find such alias for struct in C. Where does it come from ?

like image 895
IcyBrk Avatar asked Oct 25 '25 14:10

IcyBrk


2 Answers

It's not a keyword, neither in C nor Python. It's just a way to indicate that it is logically a type/class, rather than a function or method slot or the like. type is a built-in class in Python (all Python classes are instances of type, directly or indirectly), but it's not a reserved keyword; you could name a variable type if you wanted (though you'd cut off access to the original type within that scope).

In the absolute most modern versions of Python (3.12+), type is technically a "soft keyword" (meaning it's only a keyword in a specific narrow syntactic context, but usable arbitrarily elsewhere) used to enable lazy evaluation for types used in type annotations, but that usage came long after the docs began using type to indicate class types.

like image 124
ShadowRanger Avatar answered Oct 27 '25 05:10

ShadowRanger


What appears as type means different things depending on whether you're looking at documentation intended for C or Python.


The documentation source you're looking at is at cpython/Doc/c-api/type.rst, where it is written as c:type:: PyTypeObject. This is for C code, and is Sphinx syntax, which is documented here:

.. c:type:: typedef-like declaration
.. c:type:: name

Describes a C type, either as a typedef, or the alias for an unspecified type.

This indicates that PyTypeObject is either a name of a C typedef or some existing struct.


If you see type XYZ in docs intended for Python code, it would actually be py:type: in the documentation source, which only refers to the name of a Python type alias, and never the name of a class.

What is rendered as type XYZ mirrors the type statement declaration syntax, and here, in Python source code type is actually a soft keyword.

like image 40
dROOOze Avatar answered Oct 27 '25 05:10

dROOOze



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!