How can I find the methods or attributes of an object in python?

Answer Posted / chaitanya

Built-in dir() function of Python ,on an instance shows the instance variables as well as the methods and class attributes defined by the instance's class and all its base classes alphabetically. So by any object as argument to dir() we can find all the methods & attributes of the object’s class.

Following code snippet shows dir() at work :

class Employee:

def __init__(self,name,empCode,pay):

self.name=name

self.empCode=empCode

self.pay=pay

print("dir() listing all the Methods & attributes of class Employee")

print dir(e)

-----------------------------------------------------

Output

dir() listing all the Methods & attributes of class Employee

[ '__init__', 'empCode', 'name', 'pay']

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

How do you calculate the length of a string?

515


How do you write code in python?

484


Is multiple inheritances possible in python?

462


Why python is fast?

470


Explain the inheritance in python with an example?

457






What is a frozen set in python?

507


Why lambda forms in python do not have statements?

484


How will you convert an integer to a unicode character in python?

447


What is python and explain some of its benefits?

438


What is anagram in python?

426


How will you check if all characters in a string are alphanumeric?

491


How do you create your own package in python?

505


How is exception handling done in python?

473


Which laptop is best for python programming?

469


Tell us what is python? What are the benefits of using python?

444