Help me cythonize a python routine!

Steve D'Aprano steve+python at pearwood.info
Wed Nov 9 18:15:07 EST 2016


On Thu, 10 Nov 2016 10:01 am, BartC wrote:

> I haven't ruled out that collections is written in Python. But I can't
> find a 'collections.py' module in my Python 3.4; the nearest is
> "__init__.py". And there /is/ a lot of code there.

And that's exactly right.

py> import collections
py> collections.__file__
'/usr/local/lib/python3.5/collections/__init__.py'

That's because collections is a package, not just a single file module:

py> collections.__package__
'collections'


Which means it is made up of a single directory "collections", a file
collections/__init__.py which makes it a package, plus any additional
sub-modules or sub-packages under the collections directory:

py> import os
py> os.listdir('/usr/local/lib/python3.5/collections/')
['__init__.py', '__pycache__', '__main__.py', 'abc.py']


You can ignore the __pycache__ directory, that's just used for caching the
byte-code compiled .pyc files. __main__.py is used if you try to run
collections as a script:

python3.5 -m collections  # will run collections/__main__.py

and the submodule abc.py is automatically imported for you:


py> collections.abc
<module 'collections.abc'
from '/usr/local/lib/python3.5/collections/abc.py'>





-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list