Skip to content
AgileLogics
4 min read

Metaprogramming and Reflection

Reflection means a program examining or modifying its own structure while running — Python supports this more directly than many languages.

class User:
    def __init__(self, name):
        self.name = name

user = User("Ada")
print(hasattr(user, "name"))          # True
print(getattr(user, "name"))          # Ada
setattr(user, "role", "admin")
print(user.role)                       # admin

This is powerful but easy to overuse — reach for it for genuinely dynamic situations (a plugin system, a generic serializer), not as a substitute for simply naming an attribute directly.