Skip to main content

Chaining

Functions can return objects, including their own. This in turn allows for "chaining" in the same style as the core Dagger API.

Every call to a function is executed independently by the API, and thus by Python as well, even if they're in the same object. So long as the object can be JSON-serialized, its state will be preserved and passed to the next function the chain.

The following listing shows an example of function chaining, using instance attributes to share state:

"""A simple chaining example module."""
from typing import Self

from dagger import function, object_type


@object_type
class MyModule:
"""Functions that chain together."""

greeting: str = "Hello"
name: str = "World"

@function
def with_greeting(self, greeting: str) -> Self:
self.greeting = greeting
return self

@function
def with_name(self, name: str) -> Self:
self.name = name
return self

@function
def message(self) -> str:
return f"{self.greeting}, {self.name}!"

And here is an example call for this module:

dagger call with-greeting --greeting=Bonjour with-name --name=Monde message

The result will be:

Bonjour, Monde!