265-5: Supplied answer confusion

I am very confused by the supplied answer for 265-5:

class Pipeline:
    def __init__(self):
        self.tasks = []
class Pipeline:
    def __init__(self):
        self.tasks = []
        
    def task(self):
        def inner(f):
            self.tasks.append(f)
            return f
        return inner

pipeline = Pipeline()
    
@pipeline.task()
def first_task(x):
    return x + 1

print(pipeline.tasks)

Shouldn’t there be a second argument defined for the task() method?

My code (which works and is accepted by the submit answer):

class Pipeline:
    def __init__(self):
        self.tasks = []
        
    def task(self,func):
        def inner(arg):
            self.tasks.append(func)
            return func(arg)
        return inner

pipeline = Pipeline()
    
@pipeline.task
def first_task(x):
    return x + 1

print(first_task(3))
print(pipeline.tasks)

Which has the following output:

4
[<function first_task at 0x7ff6cd5cc6a8>]

I understand that my solution doesn’t actually work because my pipeline class adds tasks to the list after they are ran rather than before they are run.

Can someone explain what is happening under the surface with the supplied solution? I don’t understand how a second argument doesn’t need to be defined for the task() method.

1 Like

Please make sure to also include a link to the Mission/Mission Step in your posts (including some of your more recent ones). It’s difficult for others to have a reference and context to your question, otherwise.

1 Like

Noted. I figured including the tag # would be of most importance b/c most posts don’t, so when you look for the “Community discussion” on a given section, none of the posts can be found. It seems like most people would use the Community Discussion when looking at a particular section, not as a general search function for a data science topic…

I was able to figure it out.

Notes that should be included:

@pipeline.task()
def first_task(x):
    return x + 1

Is equivalent to:

first_task = pipeline.task()(first_task)

Which in this case means that when the code is run, the pipeline.task() method runs ---->
‘’’
first_task =inner(first_task)
‘’’
Which adds a reference to the first_task function in the self.tasks list instance variable and returns a reference to the first_task function to first_task.

The end result is that the first_task function is added to list of tasks to be run w/o needing to run first_task(). I get that this is technically already mentioned by “The @pipeline.task() decorator is called before wrapping. The first_task() function is being dynamically added to a list in the object” but more clarification would have been nice.