1

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
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526

1 Answers1

2
  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".

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • 1
    Wrt 2, most private functions are `static` so headers alone wouldn't work. Linker would complain. And if they aren't exported, they wouldn't work in an extension module anyway. – yak Apr 14 '12 at 10:13
  • … I would also add that both of the example modules the questioner cites are high-level reimplementations of structures and algorithms that are likely available in C in some fashion. I am not a C hacker myself, but I program in C++ on a daily basis – I have made use of e.g. C++ STL’s `std::deque` and `std::mutex` (which are rough analogs of `collections.deque` and `threading.Condition`, respectively) through the cpython API; you may want to explore native alternatives along these lines. – fish2000 Oct 03 '16 at 19:26