call to create_question() in polls site Django tutorial unexplained

My query is related to the part 5 of the Django Tutorial
Let me share the part of the code (from polls/tests.py) related to my query;

def create_question(question_text, days):
    #...
    time = timezone.now() + datetime.timedelta(days=days)
    return Question.objects.create(question_text=question_text, pub_date=time)

class QuestionIndexViewTests(TestCase):
        # ...
    def test_future_question_and_past_question(self):
        """
        Even if both past and future questions exist, only past questions
        are displayed.
        """
        question = create_question(question_text="Past question.", days=-30)
        create_question(question_text="Future question.", days=30)
        response = self.client.get(reverse("polls:index"))
        self.assertQuerySetEqual(
            response.context["latest_question_list"],
            [question],
        )
        #......

In the function test_future_question_and_past_question(), observe the two statements;

question = create_question(question_text="Past question.", days=-30)
create_question(question_text="Future question.", days=30)

The first statement creates a past question and saves it in a variable named ‘question’. The second statement creates a future question but does not save it.

Is not this a syntax error as the method create_question() when called returns an instance of Question type which is not received by some variable like in the first statement?

Best Regards.

Almost correct. The first statement calls the create_question function.

The create_question function:

  • Creates a past question
  • saves it in the database
  • Returns the reference to that object

Then, that first statement saves the reference to that object in a variable named question.

The second statement also calls the create_question function, which does all three of the same bullet points listed above - but then ignores the reference to the saved object that was returned.

No it is not. There is nothing in the Python syntax that requires code calling a function do anything with the return value from that function.

1 Like

Very well explained. Thank so much KenWhitesell. :saluting_face: