Skip to content

Using Vibeflow with Classes

The @vibe decorator is not limited to standalone functions. It also works with class methods. When used inside a class, the decorator provides the LLM with the full context of the class, including the __init__ method and other methods, allowing it to generate code that correctly interacts with instance attributes.

Class Method Example

Here is an example of a Counter class whose methods are entirely generated by Vibeflow.

Example: class_usage.py

from vibeflow import vibe


class Counter:
    """A simple counter class whose methods are generated by VIBE."""

    def __init__(self, initial_value: int = 0):
        self.value = initial_value

    @vibe
    def increment(self, amount: int = 1) -> None:
        """Increments the counter's value by a given amount."""
        pass

    @vibe
    def decrement(self, amount: int = 1) -> None:
        """Decrements the counter's value by a given amount."""
        pass

    @vibe
    def get_value(self) -> int:
        """Returns the current value of the counter."""
        pass


if __name__ == "__main__":
    print("--- VIBE Class Method Example ---")

    # Create an instance of the Counter class
    my_counter = Counter(initial_value=10)
    print(f"Initial value: {my_counter.get_value()}")

    # Increment the counter
    my_counter.increment(5)
    print(f"After incrementing by 5: {my_counter.get_value()}")

    # Decrement the counter
    my_counter.decrement(2)
    print(f"After decrementing by 2: {my_counter.get_value()}")

    # The final value should be 13
    print("\nFinal value should be 13. Let's check:")
    final_value = my_counter.get_value()
    print(f"Final value is: {final_value}")
    assert final_value == 13
    print("✅ Assertion passed!")

In this example, Vibeflow is aware that increment, decrement, and get_value are methods of the Counter class. It uses the __init__ source code to understand that the class has an attribute self.value, and it generates methods that correctly manipulate this attribute.