how python dir works

Terry Reedy tjreedy at udel.edu
Wed Apr 18 16:41:40 EDT 2012


On 4/18/2012 11:56 AM, cheung wrote:
> how does the function  "dir" works,
 > where can I get the python-c  source of dir in py2.7 project.

Interesting question.
The CPython source is here: hg.python.org/cpython/
 From there, select branches, 2.7, and browse.
 From there, Python/bltinmodule.c has source for non-class builtin x 
under the name builtin_x.

builtin_dir is a thin wrapper around PyObject_dir().
That is included by python.h, which includes nearly everything, 
including object.h.

With a little more looking, the code for PyObject_dir is in 
Objects/object.c after some helper functions that it uses.
That calls helper _dir_object(), which calls a more specialized helper 
if ob.__dir__ does not exist.

Even if this does not help you, I learned something about the source 
tree in tracking down the answer.

-- 
Terry Jan Reedy




More information about the Python-list mailing list