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
How does break work in python?
Is python the future of programming?
Does python use ram?
Does python support object oriented scripting?
What are some python projects for beginners?
How to set the figure title and axes labels font size in matplotlib?
Why should I learn python in 2019?
Can a beginner learn python?
What is meta class in python?
What are the different file processing modes supported by python?
How will you use python to read a random line from a file?
What are docstrings in python?
How can you sort a list?
Explain me what are the principal differences between the lambda and def?
When should you use generator expressions vs. list comprehensions in Python and viceĀversa?