https://prefect.io logo
Title
j

Jeff Kehler

03/28/2022, 8:31 AM
I seem to be having a problem passing a
Parameter
into the class constructor
__init__
of a task that has been subclassed from
prefect.Task
I am receiving a
prefect.Task
object instead of the value from the Parameter itself. I can't seem to figure out what I'm doing wrong.
s

Sylvain Hazard

03/28/2022, 8:45 AM
Parameters
should be passed as runtime field values for other tasks. Tasks are initialized at registering time which means your parameter does not have a value yet.
:upvote: 1
a

Anna Geller

03/28/2022, 9:36 AM
Exactly as explained☝️ by Sylvain. I can only add: you could retrieve the parameter value from the context in the
run()
method of your task, as the run method, in contrast to
__init__
, gets evaluated at runtime. Something like:
import prefect
from prefect import Task

class YourCustomTask(Task):
    def run(self):
        param = prefect.context["parameters"].get("parameter_name")
        print(f"Got parameter value: {param}")
🙌 1
s

Sylvain Hazard

03/28/2022, 9:43 AM
Oh I didn't know you could access all parameters through the context. Could see that being useful ! Thanks Anna !
👍 1
j

Jeff Kehler

03/28/2022, 10:08 AM
Thanks guys. That helped clear things up
👍 1