为什么“ dir()”在 python 中被命名为“ dir”?

在 Python 中有一个称为 dir的内置函数。这用于获取对象的所有属性的列表。

我知道它是做什么的,但是我对它为什么被称为 dir感到困惑。这个名称与从对象获取属性有什么关系?

8793 次浏览

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.

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

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

IIRC I named it after the DIR command in DOS.