Re: What about an EXPLICIT naming scheme for built-ins?

From: Carlos Ribeiro (carribeiro_at_gmail.com)
Date: 09/05/04


Date: Sun, 5 Sep 2004 17:41:55 -0300
To: Alex Martelli <aleaxit@yahoo.com>

Well. As said, almost done. One thing that I promise is that, whatever
comes out of this, I promise to summarize a faq-like answer to "why
are sorted() and [i]reversed() named like that".

For now IŽll focus on a single issue now:

My idiom:
reverse_list = [x for x in reversed(mylist)]

Alex's idiom:
reversed_list = list(reversed(mylist))

There is no doubt that Alex's idiom is *much* more efficient than
mine. However, I was left wondering on why didn't I come up with it
first time -- it's obvious once you see it, but still, for some
reason, the list comprehension was more intuitive for me. I don't know
about the rest of Pythoneers, specially newbies, but understanding
*why* did I came up with my version may help to illustrate the mental
model surrounding iterators.

My explanation is that iterators and list comprehensions are
conceptually similar. reversed() or (ireversed(), for that matter)
returns an iterator, so it's relatively simple to use it inside a list
comprehension.

list(), on the other hand, is a built in that takes a sequence as an
argument. And guess what? The argument does not need to be a "true"
sequence or tuple. According to the Python manual,

"list( [sequence]) -- Return a list whose items are the same and in
the same order as sequence's items. sequence may be either a sequence,
a container that supports iteration, or an iterator object. "

So almost anything will do it, but for some reason, I found the list
comprehension version to be more intuitive. Guess I have to read the
manual more often :-)

-- 
Carlos Ribeiro
Consultoria em Projetos
blog: http://rascunhosrotos.blogspot.com
blog: http://pythonnotes.blogspot.com
mail: carribeiro@gmail.com
mail: carribeiro@yahoo.com


Relevant Pages

  • Re: Python syntax in Lisp and Scheme
    ... predicates absolutely HAVE to be finite. ... The first 'sequence' argument COULD syntactically be an ... of predicates applied to a potentially unbounded/infinite sequence. ... As such sequences, in Python, are represented by iterators, we ...
    (comp.lang.python)
  • Re: Python syntax in Lisp and Scheme
    ... predicates absolutely HAVE to be finite. ... The first 'sequence' argument COULD syntactically be an ... of predicates applied to a potentially unbounded/infinite sequence. ... As such sequences, in Python, are represented by iterators, we ...
    (comp.lang.lisp)
  • Re: string upper, string lower, string subthisforthat
    ... | This requirement appears in the public draft standard (which is what ... as 'transform' can be used to modify a sequence in place ... | (i.e. one of the input iterators can be the same as the output ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Python Database Objects (PDO) 1.2.0 Released
    ... > Individual rows are not objects in PDO. ... if the .openis an generator then it must return a sequence of items ... think about python iterators. ...
    (comp.lang.python)