Python dir() Function: Use, Syntax, and Examples

Python dir() function: In this tutorial, we will learn about the dir() function in Python with its use, syntax, parameters, returns type, and examples. Submitted by IncludeHelp, on April 04, 2019

Python dir() function

The dir() function is a library function in Python, it is used to get the list of all properties and methods of an object, it accepts an object and returns a list of all inbuilt, user-defined properties, and methods without the values.

Consider the below example with sample input/output values:

Input:
class std_info:
    name = "Amit shukla"
    age = 21
    course = "B.Tech (CS)"

# printing details using dir() function
print(dir(std_info))
    
Output:
['__class__', '__delattr__', '__dict__'...
...'age', 'course', 'name']

Syntax

The following is the syntax of dir() function:

dir(object)

Parameter(s):

The following are the parameter(s):

  • object – an object whose properties and methods we want.

Return Value

The return type of dir() function is <class 'list'>, it returns a list of all properties and methods.

Python dir() Example 1: Get the list of properties, methods of an object

# python code to demonstrate example of 
# dir() function

class std_info:
  name = "Amit shukla"
  age = 21
  course = "B.Tech (CS)"

# printing return type of dir() function
print("return type of dir(): ", type(dir(std_info)))

# printing details using dir() function
print(dir(std_info))

Output

return type of dir():  <class 'list'>
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', 
'__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', 
'__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', 
'__new__', '__reduce__', '__reduce_ex__', '__repr__', '__se
tattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 
'age', 'course', 'name']   

Python dir() Example 2: Get the list of properties, methods an integer and a float object

# python code to demonstrate example of 
# dir() function

a = 10
b = 10.23

print("a has...")
print(dir(a))

print("b has...")
print(dir(a))

Output

a has...
['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', 
'__dir__', '__divmod__', '__doc__', '__eq  __', '__float__', '__floor__', '__floordiv__', 
'__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash_  
_', '__index__', '__init__', '__int__', '__invert__', '__le__', '__lshift__', '__lt__', 
'__mod__', '__mul__', '__ne__', '__neg_  _', '__new__', '__or__', '__pos__', '__pow__', 
'__radd__', '__rand__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__'  , 
'__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__', 
'__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', 
'__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', 
'__xor__', 'bit_length', 'conjugate', 'denominator', 'from_bytes', 
'imag', 'numerator', 'real', 'to_bytes']              

b has...
['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', 
'__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', 
'__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', 
'__index__', '__init__', '__int__', '__invert__', '__le__', '__lshift__', '__lt__', 
'__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__or__', '__pos__', '__pow__', 
'__radd__', '__rand__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__' , 
'__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__round__', 
'__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', 
'__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', 
'__trunc__', '__xor__', 'bit_length', 'conjugate', 'denominator', 
'from_bytes', 'imag', 'numerator', 'real', 'to_bytes']      


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.