Python dir() Function



The python dir() function is used to list out all the properties and methods, including the default properties of the specified object. Here, the object could be any module, function, string, list, dictionary, etc.

If no parameters are passed to dir(), it will return the list of names in the current local scope. When a parameter is provided, it returnn a list of valid attributes for that object without values.

In the next few section, we will be learning more about this function.

Syntax

The following is the syntax for the python dir() function.

dir(object)

Parameters

Following is the parameter of the python dir() function −

  • object − This parameter specifies the object whose properties are to be listed.

Return Value

The python dir() function returns a list of properties of the specified object.

Example

Let's understand the working of dir() function with the help of examples −

Example 1

When we print the value of the dir() without passing any arguments, we get the list of methods and attributes that are available as part of the standard library.

print("dir method without using any arguments")
print(dir())

On executing the above program, the following output is generated −

dir method without using any arguments
['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'traceback']

Example 2

If we pass an object of user-defined class to the dir() function, it will display the attributes and methods of the object. It also includes those built-in properties which are default for that particular object.

class newClass:
   def __init__(self):
      self.x = 55
      self._y = 44

   def newMethod(self):
      pass

newObj = newClass()
print("List of methods and properties of given class:")
print(dir(newObj))

The following is the output obtained by executing the above program −

List of methods and properties of given class:
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_y', 'newMethod', 'x']

Example 3

The dir() function allows us to display attributes and methods of a built-in module of Python as demonstrated in the code below.

import math
print("List of methods and properties of MATH module:")
print(dir(math))

The following output is obtained by executing the above program -

List of methods and properties of MATH module:
['__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'cbrt', 'ceil', 'comb', 'copysign', 'cos', 'cosh', 'degrees', 'dist', 'e', 'erf', 'erfc', 'exp', 'exp2', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'isqrt', 'lcm', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'nextafter', 'perm', 'pi', 'pow', 'prod', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc', 'ulp']

Example 4

If we want to inspect inherited methods and properties of parent class, then we can use the dir() function by passing the obejct of the child class.

class Pclass:
   def pMethod(self):
      pass
class Chclass(Pclass):
   def chMethod(self):
      pass

chObj = Chclass()
print("List of methods and properties of Parent class:")
print(dir(chObj))

The above program, on executing, displays the following output −

List of methods and properties of Parent class:
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'chMethod', 'pMethod']

python_built_in_functions.htm
Advertisements