.. method:: object.__dir__(self)
- Called when :func:`dir` is called on the object. A sequence must be
- returned. :func:`dir` converts the returned sequence to a list and sorts it.
+ Called when :func:`dir` is called on the object. An iterable must be
+ returned. :func:`dir` converts the returned iterable to a list and sorts it.
Customizing module attribute access
the module ``__dict__`` before raising an :exc:`AttributeError`. If found,
it is called with the attribute name and the result is returned.
-The ``__dir__`` function should accept no arguments, and return a sequence of
+The ``__dir__`` function should accept no arguments, and return an iterable of
strings that represents the names accessible on module. If present, this
function overrides the standard :func:`dir` search on a module.
self.assertIsInstance(res, list)
self.assertTrue(res == ["a", "b", "c"])
+ # dir(obj__dir__iterable)
+ class Foo(object):
+ def __dir__(self):
+ return {"b", "c", "a"}
+ res = dir(Foo())
+ self.assertIsInstance(res, list)
+ self.assertEqual(sorted(res), ["a", "b", "c"])
+
# dir(obj__dir__not_sequence)
class Foo(object):
def __dir__(self):