Hi All, What is right way to access a returned res...
# ask-community
m
Hi All, What is right way to access a returned result of a task which is a python class object? In my case I have a task that returns a python object; but attempting to access it with simple variable assignment seems to output NULL as a value for that value.
k
Could you show me what you are trying? do you do this in the Flow?
m
Yes, within the with context of the Flow, I’m accessing a task return type and using it later…
k
Oh don’t use
if
inside the Flow, use the
case
task instead. Are you looping in the flow?
👍 1
m
Thank you! but my main problem was the
sik
variable I’m expecting to hold a python object after task is run. It is coming out as
None
; any idea?
k
Ah gotcha when you log the
sik.target
it is None right?
👍 1
So I think when you log, this
sik
is not defined because the loggers are ran before the tasks are. The
Flow
block is like a build time but the tasks are executed during the runtime. You are getting
None
because you are logging the task
sik
and tasks have a proper called
target
which is where they can be saved. If you try
<http://logger.info|logger.info>(sik.test_attribute)
that does not share a name with a Task attribute, I think you will an error.
m
Yeah
sik.target
is None
So are you suggesting to modify code here then? Sorry, I kinda understood your explanation, but not sure if you’re proposing a solution?
k
There is not quite a solution unless you defer the logger statements as well. Logging statements in the Flow block are not deferred.
m
logger
statements are used here to help understand the environment execution; main objective is to realize if the
sik
object is properly instantiated with the other task above it.
k
I know but I’m saying I think you need to log it inside a Task or downstream Task instead of the Flow block.
👍 1
m
Oh got it. I’ll try other things, I just wish there was more examples I could look at tho!
k
Run this code snippet:
Copy code
from prefect import task, Flow
import prefect

@task
def abc(x):
    return x

@task
def log(x):
    logger = prefect.context.get("logger")
    <http://logger.info|logger.info>(x)
    return x

with Flow("test") as flow:
    logger = prefect.context.get("logger")
    a = abc(1)
    <http://logger.info|logger.info>(a)
    log(a)

flow.run()
The
<http://logger.info|logger.info>(a)
logs a task. The
log
task shows the value.
🙏 1