Skip to content

dir() function in Python

  • by

Python dir() function is used to get the list of the attributes and methods of any object (say functions, modules, strings, lists, dictionaries, etc). This function returns all properties and methods of the specified object, without the values.

dir(object) 

dir() function in Python

Simple example code gets all the properties and methods, even built-in properties which are the default for all objects.

object

class Person:
name = "John"
age = 25
country = "Norway"


print(dir(Person))

Output:

dir() function in Python

List

number1 = [1, 2, 3]
print(dir(number1))

Set

number = {12, 15, 18, 21}
print(dir(number))

Tuple

number = (21, 10, 81, 25)
print(dir(number))

Without Arguments: When dir() It is called without arguments and returns the list of names in the current local scope.

>>> dir()
['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__']

With a Module Argument: When dir(module) is called with a module as an argument, it returns a list of the module’s attributes, including functions, classes, and variables defined in that module.

>>> import math
>>> dir(math)
['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc']

With an Object Argument: When dir(object) is called with an object as an argument, it attempts to return a list of valid attributes for that object.

>>> class Example:
...     def __init__(self):
...         self.a = 1
...     def method(self):
...         pass
...
>>> obj = Example()
>>> dir(obj)
['__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', 'method']

Explanation

  • Local Scope: Without arguments, dir() provides the list of names that are currently defined in the local scope.
  • Module: When given a module, dir(module) lists the names defined in that module, including submodules and functions.
  • Object: When given an object, dir(object) lists the attributes and methods that the object has.

Do comment if you have any doubts or suggestions on this Python function topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading