Steve Palko
07/14/2025, 6:10 PMMarvin
07/14/2025, 6:10 PMNate
07/14/2025, 6:10 PMMarvin
07/14/2025, 6:12 PMpython
from prefect import flow
class MyClass:
@flow
@classmethod
def my_flow(cls, x):
return x * 2
@flow
def instance_flow(self, x):
return x * 3
You can use the @flow
decorator with:
1. Class methods (using @classmethod
)
2. Instance methods
3. Static methods (using @staticmethod
)
A few important points to note:
1. When using with @classmethod
, put the @flow
decorator BEFORE the @classmethod
decorator as shown in the example above.
2. Both class methods and instance methods work fine as flows, but they have slightly different use cases:
- Class methods (@classmethod
) can be called without creating an instance of the class
- Instance methods require you to create an instance first
Here's how you would use these flows:
python
# Using the class method flow (no instance needed)
MyClass.my_flow(5) # Returns 10
# Using the instance method flow
my_instance = MyClass()
my_instance.instance_flow(5) # Returns 15
This pattern can be particularly useful when you want to organize related flows within a class or when your flows need to share some class-level functionality or configuration.
Just remember that when using instance methods as flows, the instance itself needs to be serializable if you're planning to deploy the flow, as Prefect will need to serialize the entire object. If you're running into serialization issues, using class methods might be a better approach as they don't require instance state.Steve Palko
07/14/2025, 6:14 PM