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

Does python have encapsulation?

0 Answers  


How do you clear a python shell?

0 Answers  


What is print format value interpolation?

0 Answers  


How do you run a python script?

0 Answers  


Explain how to redirect the output of a python script from standout(ie., A monitor) on to a file?

0 Answers  






How can you access a module written in python from c?

0 Answers  


When do you use list vs. tuple vs. dictionary vs. set?

0 Answers  


Is del the same as remove()? What are they?

0 Answers  


How do I find the current module name?

0 Answers  


What is the meaning of @classmethod and @staticmethod ?

0 Answers  


How can files be deleted in python?

0 Answers  


Is there a difference between `==` and `is` in python?

0 Answers  


Categories