In article <200311102251.10904.aleaxit at yahoo.com>, Alex Martelli <aleaxit at yahoo.com> wrote: > >>> map(string.upper, ('ciao', u'ciao')) > ['CIAO', u'CIAO'] > > >>> map(str.upper, ('ciao', u'ciao')) > Traceback (most recent call last): > File "<stdin>", line 1, in ? > TypeError: descriptor 'upper' requires a 'str' object but received a 'unicode' > > >>> map(unicode.upper, ('ciao', u'ciao')) > Traceback (most recent call last): > File "<stdin>", line 1, in ? > TypeError: descriptor 'upper' requires a 'unicode' object but received a 'str' > > > To be honest I don't currently have any real use case that's quite like this > (i.e., based on a mix of string and unicode objects), but I DO have cases > in completely different domains where I end up coding the like of (sigh...): Actually I had exactly this case recently: I had an object that needed to store a pointer to a function for normalizing item names prior to looking them up in a dictionary, and most of the time (but not always) that function was lower(). But I wanted to handle both str and unicode, so I wrote a one-line function: def lower(x): return x.lower() > ...and we're back to wishing for a way to pass a nonlambda-callable. E.g. > a string-related example would be "order the strings in list lotsastrings > (which may be all plain strings, or all unicode strings, on different calls > of this overall function) in case-insensitive-alphabetical order". In 2.4 > _with_ the string module that's a snap: > > lotsastrings.sort(key=string.upper) Is that really alphabetical? It seems like it orders them based on the ordinal value of the characters, which doesn't work so well for unicodes. The last time I needed this I couldn't figure out how to get a reasonable case-insensitive-alphabetical order in pure python, so I used PyObjC's NSString.localizedCaseInsensitiveCompare_ instead; a pure Python solution that works as well as that one would be welcome. -- David Eppstein http://www.ics.uci.edu/~eppstein/ Univ. of California, Irvine, School of Information & Computer Science
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4