Fundamentals 8 min read

Understanding Python's dir() Function: Syntax, Parameters, and Practical Examples

This article explains Python's built‑in dir() function, covering its syntax, optional object argument, returned attribute list, and demonstrates multiple usage scenarios—including inspecting modules, classes, objects, and functions—through detailed code examples and output illustrations.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Understanding Python's dir() Function: Syntax, Parameters, and Practical Examples

The dir() built‑in function returns a list of names in the current local scope when called without arguments, or the attributes, methods, and defined types of the supplied object when an argument is provided. If the argument defines a __dir__() method, that method is invoked; otherwise, Python gathers as much information as possible about the object.

Syntax: dir([object]) → list of strings.

Parameter: object – the object, variable, or type whose attributes are to be listed.

Return value: a list of attribute names (as strings) belonging to the module or object.

Example 1 – Listing attributes of the current module and a list object:

# Get attribute list of the current module
dir()
>>> ['__builtins__', '__doc__', '__name__', '__package__', 'arr', 'myslice']
# List methods of an empty list
dir([])
>>> ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

Example 2 – Inspecting classes, objects, and functions:

class A:
    def a(self):
        pass
class A1(A):
    def a1(self):
        pass
if __name__ == '__main__':
    print('dir without arguments:', dir())
    print('dir class A:', dir(A))
    print('dir class A1:', dir(A1))
    a = A1()
    print('dir object a(A1):', dir(a))
    print('dir function a.a:', dir(a.a))

Output of the above code:

dir without arguments:
    ['A', 'A1', '__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__']

dir class A:
    ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'a']

dir class A1:
    ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'a', 'a1']

dir object a(A1):
    ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'a', 'a1']

dir function a.a:
    ['__call__', '__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__func__', '__ge__', '__get__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__self__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']

Example 3 – Using dir() to list all classes defined in a module:

import A
if __name__ == '__main__':
    print('dir module A:', dir(A))

Output:

dir module A: ['A', 'A1', '__builtins__', '__doc__', '__file__', '__name__', '__package__']

Example 4 – Finding classes in the current module:

Method 1 – Direct call in the module:

class A:
    def a(self):
        pass
class A1(A):
    def a1(self):
        pass
curModuleDir = dir()  # get dir of current file (module)
if __name__ == '__main__':
    print('dir without arguments:', dir())
    print('dir class A:', dir(A))
    print('dir class A1:', dir(A1))
    a = A1()
    print('dir object a(A1):', dir(a))
    print('dir function a.a:', dir(a.a))
    print('dir current file:', curModuleDir)

Method 2 – Import the current module under an alias and use dir() on it:

# A.py
import A as this
class A:
    def a(self):
        pass
class A1(A):
    def a1(self):
        pass
if __name__ == '__main__':
    print('dir without arguments:', dir())
    print('dir class A:', dir(A))
    print('dir class A1:', dir(A1))
    a = A1()
    print('dir object a(A1):', dir(a))
    print('dir function a.a:', dir(a.a))
    print('dir current file:', dir(this))

Method 3 – Retrieve the module object via sys.modules using the module name and call dir() on it:

import sys
class A:
    def a(self):
        pass
class A1(A):
    def a1(self):
        pass
if __name__ == '__main__':
    print('dir without arguments:', dir())
    print('dir class A:', dir(A))
    print('dir class A1:', dir(A1))
    a = A1()
    print('dir object a(A1):', dir(a))
    print('dir function a.a:', dir(a.a))
    # Use __name__ to get the current module object, then call dir on it
    print('dir current file:', dir(sys.modules[__name__]))
PythonReflectionintrospectionbuiltindir
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.