What about an EXPLICIT naming scheme for built-ins?

  • Thread starter Marco Aschwanden
  • Start date
M

Marco Aschwanden

I just read the changes for 2.4 and while scanning the list the past tense
built-ins got my attention:

sorted() - a new builtin sorted() acts like an in-place list.sort() but
can be used in expressions, as it returns a copy of the sequence, sorted.

reversed() - a new builtin that takes a sequence and returns an iterator
that loops over the elements of the sequence in reverse order (PEP 322)


sort() works in-place.
reverse() works in-place.

The past tense of sort() indicates that a copy of the sequence is returned.
The past tense of reverse() indicates that an iterator over the original
sequence is returned.

To my eyes it lacks a bit of consistency... well both past-tense functions
return something (but different somethings although the concepts behind
sorting and reversing are similar - a possible future trap for the faq).

It is a common trap that sort() and reverse() work in-place. Any tutorial
will warn you and still users fall into the trap over and over again...
One should take this as a usability weakness of the language!

<opinion>
I would like to see Python introducing a naming scheme for built-ins. Ruby
for example uses the ! to indicate an in-place function [sort() vs.
sort!()]. I know, that the exclamation mark is out of discussion but I
would appreciate to have a clear and distinct function naming (Explicit is
better than implicit).

An example but not very well thought out:

sort_inpl() -> in-place returns nothing
sort_copy() --> returns a sorted copy
sort_iter() -> returns an iterator over the original sequence
sort_copy_iter() -> returns an iterator on a copied and sorted sequence

</opinion>

What do you think about a naming scheme? Do you have any proposals/ideas?

Regards,
Marco
 
D

David Fraser

Marco said:
I just read the changes for 2.4 and while scanning the list the past
tense built-ins got my attention:

sorted() - a new builtin sorted() acts like an in-place list.sort() but
can be used in expressions, as it returns a copy of the sequence, sorted.

reversed() - a new builtin that takes a sequence and returns an iterator
that loops over the elements of the sequence in reverse order (PEP 322)


sort() works in-place.
reverse() works in-place.

How about reversed() returning a sequence, like sorted does, but adding
an iterator function called riter or reverseiter to iterate over
sequences in reverse order. That would then be similar to the iter
builtin. You could have a sortediter as well

David
 
P

Paul McGuire

David Fraser said:
How about reversed() returning a sequence, like sorted does, but adding
an iterator function called riter or reverseiter to iterate over
sequences in reverse order. That would then be similar to the iter
builtin. You could have a sortediter as well

David

Huh! I just assumed that sorted() and reversed() would both return new
lists, one sorted, and one reversed. I should think if you wanted an
iterator, you could use iter(sorted(x)) or iter(reversed(x)). But to get an
iterator over the reverse of a sequence, without making a copy of the
sequence, then this should have a different name, something like riter, or
reverse_iter.

In the current system, how does one construct a reversed list?
- revx = x[::-1]
- revx = [ i for i in reversed(x) ]
- revx = [tooth for tooth in ... ] # not a real example, just wanted to
follow up after "i for i"

(What if you wrote a sorted_iter that would iterate over an existing list in
sorted order?)

-- Paul
 
C

Carlos Ribeiro

(What if you wrote a sorted_iter that would iterate over an existing list in
sorted order?)

I wrote about it earlier today. The proper way to do it is through a
generator. This way you can sort a sequence and use just a small part
of it -- maybe just the top few items or so. I proposed the name
xsorted() to keep company with xrange(), but that's being deprecated,
so I really don't know if it's worth it at this point.


--
Carlos Ribeiro
Consultoria em Projetos
blog: http://rascunhosrotos.blogspot.com
blog: http://pythonnotes.blogspot.com
mail: (e-mail address removed)
mail: (e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,164
Messages
2,570,899
Members
47,441
Latest member
OscarSchle

Latest Threads

Top