What is Lazy, Laziness in Python/Django?

Asked

Viewed 178 times

5

I’m starting to program and reading the documentation from Django, I saw some references about lazyObject, about Django being Lazy and that Querysets sane Lazy.

I’ve done some research but I haven’t come to an understanding of how it works Lazy/Laziness and the benefit of.

1 answer

3


In the case of querysets, it means that it does not use the database until it is "spent". The example that Django itself offers clarifies well:

>>> q = Entry.objects.filter(headline__startswith="What")
>>> q = q.filter(pub_date__lte=datetime.date.today())
>>> q = q.exclude(body_text__icontains="food")
>>> print(q)

The first three lines generate 3 different querysets, but are just instructions that will be executed as soon as I go through the listing (or print it, as in the case).

Understand Lazy in this case as late.

There are other applications of this concept that are specific to your cases. For example, translations Lazy (ugettext_lazy), which are applied at string render time, rather than being executed when calling the gettext function.

In accordance with the corresponding documentation, this is useful to ensure that the translation works correctly at module level, as when defining Labels of fields in models or their verbose_name.

I suggest a reading of Django’s explanation on laziness.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.