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))

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

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