Search code examples
pythonccpythonpython-c-apipython-c-extension

Using higher-level Python constructs from C


Are there any best practices for the use of higher-level Python constructs such as threading.Condition, and collections.deque from modules written in C? In particular:

  1. Avoiding dict lookup costs, for methods and members
  2. Accessing parts of these constructs that are in C directly where possible
  3. When to reimplement desired functionality locally and not import from elsewhere in the standard library

Solution

    1. String lookups on a dict are very cheap in Python, but if desired you can cache them in a struct.

    2. There usually is no provision for doing so, since these libraries are meant to be accessed via Python and not C. It is still possible to generate your own headers that match the definitions in the C modules, but they would need to be maintained per Python version.

    3. There's no good answer for this one. It comes down to "fast" vs. "fast enough".