Home » Python

dir() function with example in Python

Python dir() function: Here, we are going to learn about the dir() function in Python with example.
Submitted by IncludeHelp, on April 04, 2019

Python dir() function

dir() function is a library function, 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, methods without the values.

Syntax:

    dir(object)

Parameter(s): object – an object whose properties and methods we want.

Return value: list – it returns a list of all properties and methods.

Example:

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

Python code to get the list of properties, methods of an object

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

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 code to get list of properties, method of an integer and a float object

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

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

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.