Is tuple unpacking incompatible with `map'?
Gordon McMillan
gmcm at hypernet.com
Wed Oct 20 14:58:57 EDT 1999
François Pinard wrote:
> Is it possible that tuple unpacking is not compatible with `map',
> or is there something I do not understand properly? Here is a
> transcript. This is with Python 1.5.2.
>
> >>> def postsort(pair):
> ... return pair[1]
> ...
> >>> map(postsort, [(1, 'un'), (2, 'deux'), (3, 'trois')])
> ['un', 'deux', 'trois']
> >>> def postsort(key, item):
> ... return item
> ...
> >>> map(postsort, [(1, 'un'), (2, 'deux'), (3, 'trois')])
> Traceback (innermost last):
> File "<stdin>", line 1, in ?
> TypeError: not enough arguments; expected 2, got 1
> >>>
>
> Maybe `map' is trying to protect me against errors I'm not doing?
> I would prefer to use tuple unpacking here, as it allows for
> cleaner writing.
Tuple unpacking is not quite as automatic as you are
assuming:
>>> def postsort((key, item)):
... return item
...
>>> map(postsort, [(1, 'un'), (2, 'deux'), (3, 'trois')])
['un', 'deux', 'trois']
cats-sank-ly y'rs
- Gordon
More information about the Python-list
mailing list