80

In Python there is a built-in function called dir. This is used to get a list of all the attributes for an object.

I understand what it does, but I am confused about why it is called dir. How is this name related to getting the attributes from an object?

Jason S
  • 184,598
  • 164
  • 608
  • 970
TM.
  • 108,298
  • 33
  • 122
  • 127

4 Answers4

104

IIRC I named it after the DIR command in DOS.

Guido van Rossum
  • 16,690
  • 3
  • 46
  • 49
  • 27
    I can't help but smile when the actual creator gives you a response. – PYB Jun 25 '19 at 12:45
  • 2
    I appreciate Guido himself gave an explaination on this and further hope that he will continue to reveal the mysteries like the one in here in other places. These are otherwise deadends basically. – XPD Jun 02 '22 at 18:26
  • What about using `ls()` as an alias for `dir()`? – Good Pen Aug 25 '23 at 10:51
68

It gives you a directory of all attributes of an object.

This is not a directory as used in file systems, but the standard usage: a listing of names or data.

interjay
  • 107,303
  • 21
  • 270
  • 254
  • 4
    I guess I can see this (and you are probably right), but it still strikes me as odd terminology. Plus, the Python docs don't mention this term anywhere. – TM. Dec 03 '09 at 19:50
  • 8
    @twneale of course they mention the `dir` function but they don't mention "directory" in any way related to it... the only instance of "directory" on that page is referring to an actual file system directory. – TM. Dec 03 '09 at 22:46
  • Sorta like a phone directory – Vinay Nov 19 '16 at 06:53
4

You're retrieving a "directory", a list of all of the stuff that's available in some resource.

Jonathan Feinberg
  • 44,698
  • 7
  • 80
  • 103
2

This answer will be most useful to those new to Python.

I am learning Python and just this morning was thinking about the builtin constants and functions that are available in the language.

I find it interesting that you only have to remember three things:

There is a 'special' builtin function dir().

There is a 'system variable' __builtins__.

dir(__builtins__) outputs a view of 'Python's builtin world'.

So what does dir mean? I've settled on this for now:

directions: supplies maps for traversing the Python Landscape.

See also:

dir(): Display a namespace's names

So if you want, you could also let dir stand for

display inside rubric

CopyPasteIt
  • 532
  • 1
  • 8
  • 22