@@ -274,35 +274,54 @@ \section{Built-in Functions \label{built-in-funcs}}
274274\end {funcdesc }
275275
276276\begin {funcdesc }{dir}{\optional {object}}
277- Without arguments, return the list of names in the current local
278- symbol table. With an argument, attempts to return a list of valid
279- attributes for that object. This information is gleaned from the
280- object's \member {__dict__} attribute, if defined, and from the class
281- or type object. The list is not necessarily complete.
282- If the object is a module object, the list contains the names of the
283- module's attributes.
284- If the object is a type or class object,
285- the list contains the names of its attributes,
286- and recursively of the attributes of its bases.
287- Otherwise, the list contains the object's attributes' names,
288- the names of its class's attributes,
289- and recursively of the attributes of its class's base classes.
290- The resulting list is sorted alphabetically.
291- For example:
277+ Without arguments, return the list of names in the current local scope. With
278+ an argument, attempt to return a list of valid attributes for that object.
279+
280+ If the object has a method named \method {__dir__()}, this method will be
281+ called and must return the list of attributes. This allows objects that
282+ implement a custom \function {__getattr__()} or \function {__getattribute__()}
283+ function to customize the way \function {dir()} reports their attributes.
284+
285+ If the object does not provide \method {__dir__()}, the function tries its best
286+ to gather information from the object's \member {__dict__} attribute, if
287+ defined, and from its type object. The resulting list is not necessarily
288+ complete, and may be inaccurate when the object has a custom
289+ \function {__getattr__()}.
290+
291+ The default \function {dir()} mechanism behaves differently with different
292+ types of objects, as it attempts to produce the most relevant, rather than
293+ complete, information:
294+ \begin {itemize }
295+ \item If the object is a module object, the list contains the names of the
296+ module's attributes.
297+ \item If the object is a type or class object, the list contains the names of
298+ its attributes, and recursively of the attributes of its bases.
299+ \item Otherwise, the list contains the object's attributes' names, the names
300+ of its class's attributes, and recursively of the attributes of its class's
301+ base classes.
302+ \end {itemize }
303+
304+ The resulting list is sorted alphabetically. For example:
292305
293306\begin {verbatim }
294307>>> import struct
295308>>> dir()
296309['__builtins__', '__doc__', '__name__', 'struct']
297310>>> dir(struct)
298311['__doc__', '__name__', 'calcsize', 'error', 'pack', 'unpack']
312+ >>> class Foo(object):
313+ ... def __dir__(self):
314+ ... return ["kan", "ga", "roo"]
315+ ...
316+ >>> f = Foo()
317+ >>> dir(f)
318+ ['ga', 'kan', 'roo']
299319\end {verbatim }
300320
301- \note {Because \function {dir()} is supplied primarily as a convenience
302- for use at an interactive prompt,
303- it tries to supply an interesting set of names more than it tries to
304- supply a rigorously or consistently defined set of names,
305- and its detailed behavior may change across releases.}
321+ \note {Because \function {dir()} is supplied primarily as a convenience for use
322+ at an interactive prompt, it tries to supply an interesting set of names
323+ more than it tries to supply a rigorously or consistently defined set of
324+ names, and its detailed behavior may change across releases.}
306325\end {funcdesc }
307326
308327\begin {funcdesc }{divmod}{a, b}
0 commit comments