Hi everyone, I am new to prefect, so maybe my ques...
# ask-community
a
Hi everyone, I am new to prefect, so maybe my question seems naive! is it possible to use @task for methods inside a class? I have a class with several methods that can be set as task.
z
Hi @Alireza Taghizadeh, the @task decorator is not meant to work with methods in classes. If the methods are
staticmethods
, it may work in some cases. The simplest workaround (assuming your class is not keeping meaningful state) would be to have a simple functional task that instantiates your class and calls the relevant method
Copy code
class MyClass:
    ...    
    def my_method(self, x):
        print(x)
@task
def call_my_class_my_method(x):
    return MyClass().my_method(x)
a
Tnx Zach, that is indeed, what I have done (even though my methods are not static, I made a similar hack to make them used as a function and use @task), but I thought maybe a more elegant way exists. Anyway, thanks a lot for your help.
👍 1