3

Possible Duplicate:
How do I find the location of Python module sources?

I dont understand how to read the code in the builtin python modules. I know how to find out whats in a module for example,

import os;

dir(os)

But when I try to look for example for the function listdir I cannot find a def listdir to read what it actually does.

Community
  • 1
  • 1
user1905410
  • 1,161
  • 2
  • 7
  • 4
  • The easiest option is to [look it up in the docs](http://docs.python.org/3/library/index.html). – Gareth Latty Dec 14 '12 at 23:51
  • @Lattyware: Where do the docs tell you the source? A few of the modules _do_ have links to the source, generally the ones that are meant as much as examples as usable code (like `asyncore`), but most do not (like `os`). – abarnert Dec 14 '12 at 23:53
  • @abarnert I took 'read what it actually does' to mean how to use it, not see the source. The question is a little unclear, but rereading it, your interpretation is probably right. – Gareth Latty Dec 15 '12 at 00:05
  • @HansEngel: That answer is way, way out of date. They added `inspect` way back in 2.1 so you didn't have to deal with "sometimes there's a `__file__`, sometimes there isn't, sometimes it's the `.pyc` and sometimes the `.py`, it may be different on different platforms, and even worse on different implementations like PyPy, etc." stuff anymore, and as of 3.0 the rules aren't even documented (beyond "it has to be whatever `inspect` expects"). – abarnert Dec 15 '12 at 00:12

1 Answers1

5

One word: inspect.

The inspect module provides several useful functions to help get information about live objects such as modules, classes, methods, functions, tracebacks, frame objects, and code objects. For example, it can help you examine the contents of a class, retrieve the source code of a method, extract and format the argument list for a function, or get all the information you need to display a detailed traceback.

It's in the standard library, and the docs have examples. So, you just print(inspect.getsource(os)), or do inspect.getsourcefile(os), etc.

Note that some of the standard-library modules are written in C (or are even fake modules built into the interpreter), in which case getsourcefile returns nothing, but getfile will at least tell you it's a .so/.pyd/whatever, which you can use to look up the original C source in, say, a copy of the Python source code.

You can also just type help(os), and the FILE right at the top gives you the path (generally the same as getsourcefile for Python modules, the same a getfile otherwise).

And you can always go to the online source for the Python modules and C extension modules. Just change the "2.7" to "3.3", etc., in the URL to get different versions. (I believe if you remove the version entirely, you get the trunk code, currently corresponding to 3.4 pre-alpha, but don't quote me on that.)

The os.listdir function isn't actually defined directly in os; it's effectively from <platform-specific-module> import * imported. You can trace it down through a few steps yourself, but it's usually going to be posix_listdir in posixmodule.c on most platforms. (Even Windows—recent versions use the same file to define the posix module on non-Windows, and the nt and posix modules on Windows, and there's a bunch of #if defined(…) stuff in the code.)

abarnert
  • 354,177
  • 51
  • 601
  • 671