The dir()
function is a built-in function in Python that returns a list of attributes and methods of an object. It is a way to list the attributes and methods of any Python object, including modules, classes, and instances of classes. In this article, we will explore the dir()
function in detail, including its syntax, return value, and various examples that demonstrate how to use it in practice.
Syntax
dir(object=None)
Parameters
object
(object): The object to list the attributes and methods of. If this parameter is not specified, the function returns a list of names in the current scope.
Return Value
The dir()
function returns a list of strings, each of which is the name of an attribute or method of the specified object.
Examples
Example 1: Listing attributes and methods of a module
In this example, we will use the dir()
function to list the attributes and methods of the math
module. The math
module is a built-in Python module that provides mathematical functions and constants, such as the value of Pi.
import math print(dir(math)) # Output: # ['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'comb', 'copysign', 'cos', 'cosh', 'degrees', 'dist', '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', 'perm', 'pi', 'pow', 'prod', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc']
Example 2: Listing attributes and methods of an instance of a class
In this example, we will use the dir()
function to list the attributes and methods of an instance of a custom class. We will create a class named Person
, which has a name attribute and a method that returns the name of the person.
class Person: def __init__(self, name): self.name = name def get_name(self): return self.name person = Person("John Doe") print(dir(person)) # Output: # ['__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__', 'get_name', 'name']
In this example, the list of attributes and methods returned by the dir()
function includes the name
attribute and the get_name()
method of the Person
class, as well as various special methods and attributes that are automatically defined for all classes in Python.
Example 3: Listing attributes and methods of a built-in object
In this example, we will use the dir()
function to list the attributes and methods of a built-in object, such as a list.
a_list = [1, 2, 3, 4, 5] print(dir(a_list)) # Output: # ['__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']
In this example, the list of attributes and methods returned by the dir()
function includes various special methods and attributes, as well as the common list methods such as append()
, pop()
, and sort()
.
Conclusion
The dir()
function is a useful tool for exploring the attributes and methods of any Python object. It is a quick way to see what an object can do and what attributes it has, which can be useful when working with new libraries or when trying to debug your code. The output of the dir()
function can be overwhelming, but you can use it in combination with other functions such as help()
or the inspect
module to get more information about specific attributes and methods. It’s also worth noting that the dir()
function only lists the attributes and methods that are accessible from the object’s namespace. Some attributes and methods may be hidden or inaccessible, and therefore will not appear in the output of dir()
.
Overall, the dir()
function is a valuable tool to have in your Python toolkit and can save you time and effort when exploring the attributes and methods of objects in your code.