Table of Contents
The dir() method tries to return a list of valid attributes of the object.
The syntax of dir()
is:
dir([object])
1. dir() Parameters
dir()
takes maximum of one object.
- object (optional) –
dir()
attempts to return all attributes of this object.
2. Return Value from dir()
dir()
tries to return a list of valid attributes of the object.
- If the object has
__dir__()
method, the method will be called and must return the list of attributes. - If the object doesn’t have
__dir__()
method, this method tries to find information from the__dict__
attribute (if defined), and from type object. In this case, the list returned fromdir()
may not be complete.
If an object is not passed to dir()
method, it returns the list of names in the current local scope.
3. Example 1: How dir() works?
number = [1, 2, 3] print(dir(number)) print('\nReturn Value from empty dir()') print(dir())
Output
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'] Return Value from empty dir() ['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'number']
4. Example 2: dir() on User-defined Object
class Person: def __dir__(self): return ['age', 'name', 'salary'] teacher = Person() print(dir(teacher))
Output
['age', 'name', 'salary']
Related posts:
Python Set isdisjoint()
Python List insert()
Python Program Read a File Line by Line Into a List
Natural Language Processing with Python - Steven Bird & Ewan Klein & Edward Loper
Python Program to Find Armstrong Number in an Interval
Python Program to Compute all the Permutation of the String
Machine Learning Applications Using Python - Cases studies form Healthcare, Retail, and Finance - Pu...
Python Program to Convert Celsius To Fahrenheit
Python Operators
Python exec()
Python String rstrip()
Python Program to Access Index of a List Using for Loop
Python Program to Create a Countdown Timer
Python Program to Shuffle Deck of Cards
Python String istitle()
Python Shallow Copy and Deep Copy
Python Program to Find Hash of File
Building Chatbots with Python Using Natural Language Processing and Machine Learning - Sumit Raj
Python Program to Illustrate Different Set Operations
Python memoryview()
Python Set symmetric_difference_update()
Python Deeper Insights into Machine Learning - Sebastian Raschka & David Julian & John Hearty
Python round()
Python sum()
Introduction to Scientific Programming with Python - Joakim Sundnes
Python Program to Find HCF or GCD
Python RegEx
Python Program to Compute the Power of a Number
Python Set symmetric_difference()
Python int()
Python pass statement
Python String isdigit()