Hi. Is it any difference in task calling syntax? `...
# ask-community
v
Hi. Is it any difference in task calling syntax?
Copy code
class SetupStorageTask(Task): 
    def run(self, mode) -> Storage:    
        return Storage(mode=mode)

setup_storage_task = SetupStorageTask()
storage = setup_storage_task(mode='rw')
and
Copy code
class SetupStorageTask(Task):

    def __init__(self, mode):
        self.mode = mode
    
    def run(self) -> Storage:
        return Storage(mode=self.mode)

setup_storage_task = SetupStorageTask(mode='rw')
storage = setup_storage_task()

or can be just
storage = SetupStorageTask(mode='rw')
a
Both can work, it depends on how you want to use this task. E.g. if you want to pass some data into the task, then it makes sense to accept this argument within the “run” method rather than only on init. Many tasks in our task library set all attributes as optional allowing them to be set both at initialization and within the Flow constructor and include some logic within the task itself to check whether the required values are set and if not raise ValueError. I think if you have a look at a couple of tasks in the task library code, you’ll get a better grasp of how it works.
@Vadym Dytyniak but on the other note: do you wish to use it to set Flow storage? if so, I think this won’t work, because Storage doesn’t inherit from Task
v
no, it is project related storage
but what about
Copy code
setup_storage_task = SetupStorageTask(mode='rw')
storage = setup_storage_task()

or can be just

storage = SetupStorageTask(mode='rw')
looks like both versions works
if run does not have args
you can just use storage = SetupStorageTask(mode='rw')
is it true?
k
It has to be :
Copy code
storage = SetupStorageTask(mode='rw')()
The first parenthesis is the
init
and the second is the
run
v
but it works without last ()
a
yes, you can. Again, I think you’ll get a better feel for it when you look at some existing tasks, there are many that use a similar pattern but if you’re asking how it should be called within Flow, then as Kevin said:
Copy code
with Flow() as flow:
   storage = SetupStorageTask(mode='rw')()
or initialize first and then call in a flow:
Copy code
xyz = SetupStorageTask(mode='rw')
with Flow() as flow:
    xyz()
v
It makes sense I just was surprised that without last () it do the same thing
probably just sideeffect
run without args