Python dir() Function

This guide deals with the Built-in Python dir function and it’s uses, complete with several examples.

The Python dir() function has the ability to retrieve a list of all attributes and methods belonging to any object. If no object is specified, the dir() function will return all the module names in the local namespace. The list of attributes, methods and modules returned is always in alphabetical order.

The dir function has two main uses. For one, it’s used by Developers to keep track of all the attributes and methods they have. This becomes critical in larger projects where good maintainence and documentation are key.

Secondly, a more common usage, the dir function can output the attributes and methods for objects like lists, tuples and dictionaries. If you have little knowledge about an object, the dir function will reveal all it’s secrets to you.


Syntax

The dir function can be called either without any parameters, in which case it defaults to the local namespace. Otherwise, you can pass an object into it’s parameters and the dir function will focus on that.

dir(object)

Keep in mind, the dir function only works on those objects with the __dir__ method.


Example 1:

To demonstrate it’s use with the local namespace, we’re going to call the dir function twice. In between, we’re going to add more modules to the local namespace.

print(dir())

import numpy
import os
import sys

print(dir())
['__annotations__', '__builtins__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'argv']

['__annotations__', '__builtins__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'argv', 'numpy', 'os', 'sys']

Take a good look at the two outputs and compare them. You’ll notice that there are now three new module names in the second output.

Example 2:

We’ll be printing out the list of attributes and methods in this list. The contents of the list do not matter right now. The functions will remain the same regardless of the values in the object.

X = ["Apple", "Orange", "Grape"]

print(dir(X))
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

If you look carefully at the output, you’ll see many built-in functions that you’ve probably used with lists. The append, remove, reverse and insert functions for instance are fairly common.


This marks the end of the Python dir Function Article. Any suggestions or contributions for CodersLegacy are more than welcome. Questions can be asked in the comment section below.

Here’s a link back to the main Python Built-in Functions page.