How to use the dir() function in Python?

In Python, you can use the dir() function to list all attributes and methods of a specified module, class, method, or object.

For example, in an interactive interpreter, you can use the dir() function to see all attributes and methods of the built-in module math.

import math
print(dir(math))

The output will list all available attributes and methods in the math module.

In addition, you can also use the dir() function to view all the attributes and methods of a specified object. For example:

s = "Hello"
print(dir(s))

This will list all the available properties and methods of the string object s.

It is important to note that the dir() function will return a list containing the names of attributes and methods.

 

More tutorials

Python functions for calculating logarithm(Opens in a new browser tab)

ValueError exception in Python code examples(Opens in a new browser tab)

Installation of Arch Linux(Opens in a new browser tab)

Python Substring refers to extracting a smaller portion of a string.(Opens in a new browser tab)

How to include items to a list in Python(Opens in a new browser tab)

Leave a Reply 0

Your email address will not be published. Required fields are marked *