Courses/Metaprogramming

๐Ÿง™ Metaprogramming

Dynamic Attribute Access

getattr, setattr, hasattr at runtime.

Attribute names aren't fixed โ€” you can read and write them dynamically.

class Bag: pass

b = Bag()
setattr(b, "color", "red")
print(getattr(b, "color"))            # red
print(getattr(b, "missing", "n/a"))   # n/a
main.py
Output
Press Run to execute.
Expected output
localhost
8080

Sign in to track your progress across lessons.