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?
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.
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.
Most likely it's a reference to the DIR
command of MSDOS. DIR does directory listings, like the Unix command ls
.
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]
精彩评论