Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
functions
communitycreator

What is the dir() function in python?

NDUKWE CHIDERA K.

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

Python boasts a wide range of modules and functions in its standard library. These functions make writing code with python fast and fun. Just by importing these modules into your script, you can access all of its methods. One of these built-in modules is dir.

What is dir()?

This is a python function that returns a properly arranged list of strings that contains all the names of attributes and methods defined by an object such as a module, class, function, string, list, dictionary, and so on.

Syntax

dir(object)

Parameters

  • object: this is the module, function, class, etc, whose properties and other names that are defined in it are to be returned. By default, here means properties and methods are made automatically available to an object upon its definition. This parameter can be any module, class, or function at all – either user-defined or built-in.

Return Value

When called on an object, dir() will return valid attributes and names in such object.

With different types of objects comes different behaviors by the dir() function. This is because the function strives to return the relevant names in the list rather than all the possible information that it could return.

  • For class objects: A valid list of names that contains all attributes and base attributes of this class are returned by the dir() function.

  • For modules/library objects: All attribute names contained in the module and library object are returned.

  • If there is no parameter provided, it returns a list of names in the current local scope.

Code

Example 1

In the code below, the dir() function was used to return a list of all names contained in the built-in OS module.

import os
print(dir(os))

Example 2

In this second code snippet, a user-defined class is passed as an argument to the dir() function. From the code output, it is clear that both the methods made available by default to all classes defined in python and the user-defined properties of the class were all returned.

class Clothe:
name = "Men V-neck Tshirt"
size = 36
color = "blue"
print(dir(Clothe))

RELATED TAGS

python
functions
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring