On Dec 2, 2007 7:31 AM, Malcolm Tredinnick <[EMAIL PROTECTED]> wrote:
>
>
> On Fri, 2007-11-30 at 20:00 -0800, [EMAIL PROTECTED] wrote:
> > To clarify, code such as the following can be a little confusing:
> >
> > p = MyModel.objects.filter(foo__id = self.id).order_by('-timestamp')[:
> > 1]
> > if p:
> >     return p[0]
> >
> > To a python developer who is unfamiliar with django's magic limiting
> > syntax, the slice there looks unnecessary.
> >
> > Writing objects.filter(...).order_by(...).limit(1) would be clearer
> > and significantly less error prone.
>
> No it wouldn't. It would be differently error-prone.
>
> Using Pythonic syntax is nice and it's pretty rare that adding the slice
> is really going to affect performance, since it only queries the
> database if you haven't *already* run the database query (if you slice a
> queryset that is already cached, it pulls the results out of the cache).
>
> I'd be -1 on changing this.
>
> Malcolm

I may be alone in this, but the slicing syntax actually seems a bit
non-Pythonic to me.  Query sets are conceptually similar in many ways
to iterators, yet iterators (at least the ones in the standard
library) are not slicable.  In order to do that, you have to use
itertools.islice, a function call.  Similarly, to reverse an iterator,
you don't slice it with [::-1]; you call reversed, another function.

It seems to me the difference is that slicing, like attribute lookup,
is intuitively a simple operation that works on data already in
memory.  If the operation is going to do something substantially more
complex than that, as QuerySet does, then implementing it using
__getitem__ or a property is just misleading; it's actually a method,
and it ought to be denoted as such.

In short, the fact that it uses a feature of Python doesn't
necessarily make it Pythonic in my view.  Count me as +0 on the
change.

-root

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-developers@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to