Python heapq - Python and C implementation? Which one is used?

I am looking at a Python source and notice the C implementation for heapq , as well as the Python implementation . Why are they both? Which one is used when I import heapq from CPython?

+6
source share
1 answer

import heapq imports a Python implementation. You can confirm this by checking the heapq value in the interpreter:

 In [20]: import heapq In [21]: heapq Out[21]: <module 'heapq' from '/usr/lib/python2.7/heapq.pyc'> 

heapq.pyc is a byte-compiled version of the heapq.py module.

However, inside the heapq.py file:

 # If available, use C implementation try: from _heapq import * except ImportError: pass 

_heapqmodule.c provides the _heapqmodule.c module. Therefore, if a C implementation is available, import heapq uses a C implementation.

+6
source

Source: https://habr.com/ru/post/984214/


All Articles