Menu Close

Python dir Function

In this tutorial, we are going to learn all about the Python dir() function to getting all the properties and methods of an object.
Python dir method is the best python built-in function when you want to access all the methods and properties of any objects.

In the previous python built-in functions tutorial, we have seen the Python dict function to create the dictionary.

Python dir Function

Python dir function is a Python built-in function, that is used to returns all the properties and methods of the objects, without the values.

This function will return all the properties and methods even all the built-in properties which are the default for all the objects.

Syntax

The syntax of dir function in python is:-

dir(object)

Parameter

dir function in python accepts one parameter that is called object:-

  • object:- The object you want to see the valid attributes of.

Return Value

Return value of Python dir method is all properties and methods.

Python dir function examples

Here we will take some examples to understand the python built-in dir function.

Example:

class Person:
	age = 21
	name = 'John'
	city = 'California'
	
	def printNow(self):
		print(self.name +  ' ' + self.city)


#Create instance of Person class
p1 = Person()

#access printNow() method using p1 object.
p1.printNow()

#access all the properties and methods of the p1 object.
print(dir(Person))

Output

John California
['__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__', 'age', 'city', 'name', 'printNow']

List object properties and methods

To access all the properties and methods of the list object, you can use the python dir() function.

Example:

x = dir(dict)
print(x)

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']

Dictionary object properties and methods

To access all the properties and methods of the dictionary object, you can use dir() function.

Example:

print(dir(dict))

Output

['__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']

Conclusion

In this tutorial, you have learned all about the python dir() function to get all the properties and methods of any object.
Using the Python dir method, you can find all the properties and methods of your own objects.

If this article helped you, please keep visiting for further python built-in functions tutorials.

Other Python built-in functions

For more information:- Click Here

Previous Article
Next Article

Was this post helpful?

Related Posts

Leave a Reply