I'm with Shai, against changing bool() and len() behaviour. I think the
"fetch the whole queryset" behaviour is normally helpful for beginners, who
write things like:
if qs:
for x in qs:
...
# really common for anyone new to Python:
for i in len(qs):
do_something(qs[i])
We have highlighted this behaviour for a long time in the 'database access
optimization' page really well:
https://docs.djangoproject.com/en/3.0/topics/db/optimization/#use-queryset-count
. There are alternatives if you want different behaviour - exists() and
count() .
+1 to .contains() since it would follow that same pattern.
On Fri, 5 Jun 2020 at 07:03, Shai Berger <[email protected]> wrote:
> On Tue, 2 Jun 2020 20:57:54 +0200
> Aymeric Augustin <[email protected]> wrote:
>
> >
> > We're talking about a trade-off between preserving optimisations in
> > existing code bases and expertise of advanced users versus doing the
> > right thing by default for less experienced users.
>
> I disagree.
>
> The suggestion is to make
>
> if obj in qs:
> # do things which don't fetch qs
>
> more performant, at the expense of
>
> if obj in qs:
> # do things that fetch qs
>
> As well as
>
> for obj in container:
> if obj in qs:
> # whatever
>
> So, it is not a net optimization even in terms of its own.
>
> Also, changing the performance characteristics of thousands, if not
> millions of existing lines of code, and making a decade of online
> comments and advice wrong (not just obsolete "there's better ways now"
> but wrong "this points you away from truth and good"), is IMO a
> non-starter.
>
> Strong -1 on Aymeric's option 1. Strong +1 on adding ".contains()".
>
> My 2 cents,
> Shai.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django developers (Contributions to Django itself)" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-developers/20200605090349.559f3872.shai%40platonix.com
> .
>
--
Adam
--
You received this message because you are subscribed to the Google Groups
"Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-developers/CAMyDDM1s8moPUY0dqfm3bcnFF9a7ATfz%3DDjSW2p-uXSMqvwcAg%40mail.gmail.com.