Fundamentals 5 min read

Python Reflection and Dynamic Attributes: 10 Practical Scenarios

This article explains Python's reflection mechanism and dynamic attributes, presenting ten practical code examples that demonstrate how to inspect, modify, create, and delete object properties and methods at runtime for more flexible and reusable programs.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Python Reflection and Dynamic Attributes: 10 Practical Scenarios

In Python, reflection allows a program to inspect and modify objects' attributes and methods at runtime, while dynamic attributes let developers add, change, or remove attributes on the fly. The following ten examples illustrate common use‑cases.

1. Getting an object's attributes and methods: Use dir() to list all members and getattr() to retrieve a specific attribute or method.

class MyClass:
    def __init__(self):
        self.my_attribute = 42

    def my_method(self):
        return "Hello, World!"

obj = MyClass()
attributes = dir(obj)
attribute_value = getattr(obj, "my_attribute")
method_result = getattr(obj, "my_method")()

2. Checking for the existence of an attribute or method: Use hasattr() .

class MyClass:
    def __init__(self):
        self.my_attribute = 42

obj = MyClass()
has_attribute = hasattr(obj, "my_attribute")
has_method = hasattr(obj, "my_method")

3. Dynamically setting attributes or methods: Use setattr() .

class MyClass:
    pass

obj = MyClass()
setattr(obj, "my_attribute", 42)

def my_method(self):
    return "Hello, World!"
setattr(obj, "my_method", my_method)

4. Dynamically deleting attributes or methods: Use delattr() .

class MyClass:
    def my_method(self):
        return "Hello, World!"

obj = MyClass()
delattr(obj, "my_method")

5. Modifying class attributes and methods at runtime: Apply setattr() to the class itself.

class MyClass:
    my_attribute = 42
    def my_method(self):
        return "Hello, World!"

setattr(MyClass, "my_attribute", 100)
setattr(MyClass, "my_method", my_method)

6. Dynamically creating a class: Use the built‑in type() function.

MyClass = type("MyClass", (object,), {"my_attribute": 42})
obj = MyClass()

7. Dynamically modifying an object's behavior: Assign new attributes or methods directly.

class MyClass:
    pass

obj = MyClass()
obj.my_attribute = 42

def my_method(self):
    return "Hello, World!"
obj.my_method = my_method

8. Dynamically changing an object's attribute value: Simple reassignment.

class MyClass:
    my_attribute = 42

obj = MyClass()
obj.my_attribute = 100

9. Dynamically deleting an object's attribute: Use the del statement.

class MyClass:
    my_attribute = 42

obj = MyClass()
del obj.my_attribute

10. Dynamically invoking an object's method: Call the method after it has been added.

class MyClass:
    def my_method(self):
        return "Hello, World!"

obj = MyClass()
method_result = obj.my_method()

These examples demonstrate how reflection and dynamic attributes provide powerful, flexible ways to manipulate objects, but developers should use them judiciously to maintain code readability and avoid excessive complexity.

PythonProgrammingreflectionintrospectiondynamic attributes
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.