Remember
dir() lists names associated with an object. Its result is useful for exploration, not a guarantee about every possible dynamic attribute.
dir() lists names associated with an object. Its result is useful for exploration, not a guarantee about every possible dynamic attribute.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
items = [3]
names = dir(items)
print("append" in names)
print("items" in names)Top-level scope
[3]Object 2 · list['__add__', '__class__', '__class_getitem__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', …]dir() lists names associated with an object. Its result is useful for exploration, not a guarantee about every possible dynamic attribute.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
items = [3]
names = dir(items)
print("append" in names)
print("items" in names)
|
v
DISPLAYED RESULT
True
False
FINAL BINDINGS
items --> object 1 (list)
names --> object 2 (list)
object 1: [3]
object 2: ['__add__', '__class__', '__class_getitem__', '__contains__', ...Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
items = [3]
names = dir(items)
print("append" in names)
print("items" in names)Top-level scope
[3]Object 2 · list['__add__', '__class__', '__class_getitem__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', …]The example has finished. Compare the final output with the values and references above.
True False
What do you expect this program to print?
items = [3]
names = dir(items)
print("append" in names)
print("items" in names)dir() FunctionThe dir() function is a built-in Python tool that returns a list of valid attributes (including methods) for any object. It's often used for exploring objects during development or debugging.
dir([object])
object (optional) – the object whose attributes you want to listWhen used without arguments, dir() returns the names in the current local scope.
x = 10
y = "hello"
print(dir())
['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'x', 'y']
print(dir("hello"))
['__add__', '__class__', '__contains__', 'capitalize', 'casefold', 'center', 'count', ...]
This shows all the string methods and internal attributes.
import math
print(dir(math))
['__doc__', '__loader__', 'acos', 'asin', 'atan', 'ceil', 'cos', 'pi', 'sqrt', ...]
__getattr__ may not appearUse dir() along with type() and help() for debugging and exploring unfamiliar objects during coding interviews or while using third-party libraries.
dir() helps inspect available attributes and methods of objectsTry running dir() on a few different objects like [], {}, set(), and len. Observe how their available methods and properties differ.
print("List Methods:", dir([]))
print("Dict Methods:", dir({}))
print("Set Methods:", dir(set()))
print("Function Methods:", dir(len))
Connect what you noticed in the example with the idea below.
dir() lists names associated with an object. Its result is useful for exploration, not a guarantee about every possible dynamic attribute.
dir() changes the object’s attributes.
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.