Search code examples
pythonairflowcontextmanager

Why leave the variable at the end of the with statement?


I am trying to make sense of the why you would just write a particular variable at the end of the with statement ?

from datetime import datetime, timedelta

from airflow import DAG
from airflow.operators.bash import BashOperator

default_args = {
    'owner': 'coder2j',
    'retries': 5,
    'retry_delay': timedelta(minutes=5)
}

with DAG(
    default_args=default_args,
    dag_id="dag_with_cron_expression_v04",
    start_date=datetime(2021, 11, 1),
    schedule_interval='0 3 * * Tue-Fri'
) as dag:
    task1 = BashOperator(
        task_id='task1',
        bash_command="echo dag with cron expression!"
    )
    task1 # What does this mean ?

This example is taken from here.

Why just leave the task1 variable at the end ?


Solution

  • You are right, there is no reason for it. Also variable task1 with the assignment could just be removed. Just:

    with DAG(
        default_args=default_args,
        dag_id="dag_with_cron_expression_v04",
        start_date=datetime(2021, 11, 1),
        schedule_interval='0 3 * * Tue-Fri'
    ) as dag:
        BashOperator(
            task_id='task1',
            bash_command="echo dag with cron expression!"
        )