Depricated String Functions in Python

riquito at gmail.com riquito at gmail.com
Thu Jul 20 10:02:55 EDT 2006


Steve Holden ha scritto:

> Anoop wrote:
> > Thanks Stefen
> >
> > let me be more specific how would i have to write the following
> > function in the deprecated format
> >
> > map(string.lower,list)
> >
> To avoid the deprecated usage you would use the unbound method of the
> str type (that's the type of all strings):
>
>   >>> lst = ['Steve', 'Holden']
>   >>> map(str.lower, lst)
> ['steve', 'holden']
>   >>>

This isn't exactly equal to use string.lower, because this work with
just encoded strings, when string.lower works with unicode too.
I'm used to have a "lower" func like this one
def lower(x): return x.lower()
to use in map. Of course it's a problem when you need many different
methods.

A solution could be something like this
>>> def doit(what):
...    def func(x):
...       return getattr(x,what)()
...    return func
...
>>> map(doit('lower'),['aBcD',u'\xc0'])
['abcd', u'\xe0']
>>> map(doit('upper'),['aBcD',u'\xc0'])
['ABCD', u'\xc0']

The best is to use in advance just unicode or encoded strings in your
program, but this is not always possible :-/

Riccardo Galli




More information about the Python-list mailing list