How to Use dir() Function in Python: Syntax and Examples
The
dir() function in Python returns a list of names (attributes and methods) of an object or the current scope if no object is given. Use dir(object) to explore what properties and functions an object has.Syntax
The dir() function can be used in two ways:
dir(): Returns a list of names in the current local scope.dir(object): Returns a list of valid attributes and methods for the givenobject.
This helps you see what you can access or use with that object.
python
dir() dir(object)
Example
This example shows how to use dir() with a string object to list its methods and attributes.
python
sample = "hello" print(dir(sample))
Output
[
'__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__',
'__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__',
'__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__',
'__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__',
'__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode',
'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum',
'isalpha', 'isascii', 'isdigit', 'islower', 'isnumeric', 'isprintable', 'isspace',
'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition',
'removeprefix', 'removesuffix', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition',
'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase',
'title', 'translate', 'upper', 'zfill'
]
Common Pitfalls
One common mistake is expecting dir() to show all attributes including those dynamically added or private ones. It shows most but not all. Also, dir() returns names as strings, not the actual values or functions.
Another pitfall is calling dir() without an object and expecting it to show global variables outside the current scope.
python
class MyClass: def __init__(self): self.x = 10 obj = MyClass() print(dir(obj)) # Shows 'x' but not its value # Wrong expectation: print(dir()) # Only shows names in current local scope, not globals outside
Output
[
'__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__',
'__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__',
'__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__',
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__',
'__str__', '__subclasshook__', '__weakref__', 'x'
]
['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__']
Quick Reference
Use this quick guide to remember how dir() works:
| Usage | Description |
|---|---|
| dir() | List names in current local scope |
| dir(object) | List attributes and methods of the object |
| Returns | List of strings naming attributes and methods |
| Does not show | Values of attributes or dynamically added names |
| Helpful for | Exploring unknown objects or debugging |
Key Takeaways
Use
dir(object) to see what attributes and methods an object has.dir() without arguments lists names in the current local scope.dir() returns names as strings, not their values or implementations.It helps explore objects but does not show all dynamic or private attributes.
Use
dir() to understand what you can access or call on an object.