0

I was reading the Django documentation, and I came across this code:

from django.db import models

class Person(models.Model):
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    birth_date = models.DateField()

    def baby_boomer_status(self):
        "Returns the person's baby-boomer status."
        import datetime
        if self.birth_date < datetime.date(1945, 8, 1):
            return "Pre-boomer"
        elif self.birth_date < datetime.date(1965, 1, 1):
            return "Baby boomer"
        else:
            return "Post-boomer"

What is the purpose of putting a string below a function definition i.e baby_boomer_status()?

1

1 Answer 1

2

That is called the docstring. Is not only for code commenting, but also can be accessed from the .__doc__ method to check info about the funcion:

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for your answer but the examples you provided in your answer used explicit comments. On the other hand, the example I gave uses a string. Is there a difference?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.