[Python-Dev] list and string - method wishlist

Alex Martelli aleax at aleax.it
Thu Nov 27 05:39:10 EST 2003


On Thursday 27 November 2003 11:16 am, Kepes Krisztian wrote:
   ...
> try:
>    i=s.index('a')
> except:
>    i=-1
> if i<>-1: pass
>
> and not this:
>
> if (s.find('a')<>-1): pass

Why don't you use the clearer, faster, more readable, AND more concise idiom
    if 'a' in s: pass
instead?

> Why don't exists same method in the list object ?

The 'in' operator works just fine for lists, too.

Perhaps if you studied Python's present capabilities a bit better, before
requesting changes and additions to Python, you might achieve better
results faster.


> Same thing is the deleting.
>
> I think, this method is missing from strings, and lists.

Look at the 'del' keyword (and slice assignments) -- for lists only:

> print l
> l[2]=None
> l.remove(None)

del l[2]

or equivalently

l[2:3] = []

> and delete more:
> s.delete()  # s=''

Python strings are immutable and will always remain immutable.  There is
NO way to change an existing string object and there will never be.

> l.delete()  # l=[]

l[:] = []

or equivalently

del l[:]

> s.delete(2,2) # s='abef'

Ditto.

> l.delete(2,2) #  l=[1,2,4,5]

l[2:4] = []

or equivalently

del l[2:4]


> So: some functions/methods are neeeded to Python-like programming
> (less write, more effectivity).

This is quite possible, but I have seen almost none listed in your wishlist.
I.e., the only task you've listed that is not performed with easy, popular and
widespread Python idioms would seem to be a string method roughly equivalent
to the function:

def delete(s, from, upto=None):
    if upto is None: upto = from + 1
    return s[:from] + s[upto:]

returning "a copy of s except for this slice".  However, the addition of more
functions and methods that might (perhaps) save typing a few characters,
allowing a hypothetical

z = s.delete(a, b)

in lieu of

z = s[:a] + s[b:]

must overcome a serious general objection: as your very request shows,
people ALREADY fail to notice and learn a lot of what Python offers today.

Adding more and more marginally-useful functions and methods might therefore 
more likely just cause people to fail to notice and learn a larger fraction 
of Python's capabilities, rather than supply any burningly needed usefulness.


Alex




More information about the Python-Dev mailing list