What does Python's dir() function stand for?

Question:

I know that the dir() function gives you back either the names defined in the current scope or the names defined in an object. But why is it called dir()? Is it some mysterious acronyms like LISP’s CAR and CDR?

Asked By: Y.H Wong

||

Answers:

It’s probably just an analogy to directory listing. list() is used for creating a lists, so dir() is used for listing elements object which has a similar tree-like structure to file system.

Just a guess.

Answered By: Michał Šrajer

It gives you an alphabetical listing of valid names (attributes) in the scope (object). This is pretty much the meaning of the word directory in english.

Answered By: wim

Most likely it’s a reference to the DIR command of MSDOS. DIR does directory listings, like the Unix command ls.

Answered By: Petr Viktorin

I know that without arguments, it returns a list of names in the current local scope. For example:

>>>z = 3
>>>def f1():
...    x = 1
...    y = 2
...    print dir()
...
>>>f1()
['x','y']
>>>print dir()
['z',something else]

With arguments, it returns a sorted dictionary keys of all the attributes of that object. for example:

>>>import sys
>>>dir(sys)
[a bunch of attributes of sys]
Answered By: zleung
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.