When using classes with @tasks, I'm assuming that ...
# prefect-community
j
When using classes with @tasks, I'm assuming that the class needs to be instantiated each time for each task? For example:
Copy code
@task
def task_a():
    common_class = Common(Secret())
    output = common_class.task_a()
    return output

@task
def task_b():
    common_class = Common(Secret())
    output = common_class.task_b()
    return output
k
I don’t think so. It would just get serialized along with the task. Do you get an error?
a
also, are you asking because of Prefect Secrets?
j
Yeah, I think I've figured out the class error. I have an API abstraction module that I'd like to use across the tasks, and it takes in a token and secret, so I was curious about the best practice. I like to use class organization instead of using many different methods when possible.
a
Gotcha. You can definitely combine functional tasks with your custom classes. Many users use
PrefectSecret
tasks in their flows and pass the value of the secret to a downstream task (which may use classes inside that receive the input data from a task) - this would allow you to get the best of both worlds. LMK if you need an example
👍 1