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



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

Answer / 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

More Python Interview Questions

Which is best python or r?

0 Answers  


Is there any tool used to find bugs or carrying out static analysis?

0 Answers  


Is it easier than Django bottle?

0 Answers  


Is there any way to kill a thread in python?

0 Answers  


How does the python version numbering scheme work?

0 Answers  






What is the use of pycharm?

0 Answers  


Give one example for multiple statements in single statement?

0 Answers  


What is the purpose of doc strings in python?

0 Answers  


How to walk through a list in a sorted order without sorting the actual list?

0 Answers  


What are advantages of a tuple over a list?

0 Answers  


What is string slicing in python?

0 Answers  


What is set when we have to use?

0 Answers  


Categories