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.