2

Can anyone explain me the meaning of “lazy”. I can’t understand the meaning of the word “lazy” in Django.

For example. In Lazy Translation Topic.

These functions store a lazy reference to the string or lazy suffix

I met that word many times in many programming languages but I can’t catch it.

Sayse
  • 42,633
  • 14
  • 77
  • 146
Jora Karyan
  • 66
  • 1
  • 9

1 Answers1

1

It means that it isn't evaluated until you need results from it.

take for example

a = iter([1,2,3])

You know that you have a list of 1,2,3 but you won't get any values from it until you need to use them in something.

for value in a:
      print(a)

For django specifically, it normally does this when involving the ORM to make queries.

You could write

 n = MyObject.objects.filter(x='foo')

but you might want to chain this query on with other additional filters later on

if x:
     n = n.filter(y="bar")

so to avoid 2 queries happening here, django doesn't attempt to fetch objects from the database until you start trying to do something with the queryset that would involve using the objects returned from the query

for db_item in n:
     print(db_item)
Sayse
  • 42,633
  • 14
  • 77
  • 146