dir() lists names and attributes available in a namespace or associated with an object, module, or type. Use it as a quick inventory when exploring unfamiliar code, checking members, or debugging. section 2.4
Use the argument form that matches what you want to inspect:
- Call dir() without arguments to list names in the current interactive namespace. section 2.4
- Call dir(module) to list names defined by a module, or dir(type) to inspect methods and other members associated with a type. section 2.4 section 10.7.1
- Treat the result as a name list rather than a display of current values or namespace settings; use help() for targeted documentation. section 2.4 section 10.7.1
Inspecting a namespace and a type
dir() # names currently available, including variables and interactive helpers dir(int) # integer methods and attributes, including __add__, __bool__, and __doc__ section 2.4
Finding a mistyped name
x1 = 6 xl = x1 - 2 dir() # reveals both x1 and xl, making the lowercase l visible section 10.7.1
dir() ['In', 'Out', '_', '__', '___', '__builtin__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', '_dh', '_i', '_i1', '_i2', '_ih', '_ii', '_iii', '_oh', 'exit', 'get_ipython', 'quit', 'x'] dir(int) ['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__', '__ge__', '__getattribute__', ... (continues with more items... ]
Compare the inventories returned by dir() and dir(int), including interactive names and integer members. section 2.4
