Question about consistency in python language

Mike Meyer mwm at mired.org
Fri Sep 9 16:32:59 EDT 2005


"Kay Schluehr" <kay.schluehr at gmx.net> writes:
> If you define
>
>>>> sep = ""
>>>> sep.join(["d","o","g"])
> "dog"
>>>> sep
> ''
>
> sep is preserved and a new "dog" string is generated. Since sep is
> immutable there is no way to manipulate it inplace.
>
> On the other hand there exists no sorted() method for tuples or lists
> like join() for strings but it is implemented as a function in Python24
> that returns a new sorted container. I consider this as an
> inconsistency across builtin types. Consistent would be following usage
> pattern:
>
>>>> l = [1,3,2]
>>>> l.sorted()
> [1,2,3]             # new sorted list
>>>> l.sort()        # sort list inplace
>>>> l.appended(4)   # new extended list
> [1,2,3,4]
>>>> l.append(4)     # appends an element to the same list
>>>> l
> [1,2,3,4]


Yes, but the function "sorted" is more useful than a list method
"sorted" in a duck typing language.

The function sorted works on all iterators. I can do:

>>> def t(n):
>>>   for i in range(n):
>>>     yield i
>>> ...
>>> print sorted(t(5))

and have it work.

If sorted were a method of a class - the it'd have to be implemented
again for every class iterable class. Either that, or you'd have to
create an abstract parent of all iterable classes to add it to - which
seems more appropriate for a B&D language than Python.

And even if you do add the abstract class, how do you make my example
work without explictly converting the iterator to a list type?

     <mike
-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.



More information about the Python-list mailing list