From: Terry Reedy <tjre...@udel.edu>
Date: Thu, 13 Sep 2012 15:11:22 -0400
Local: Thurs, Sep 13 2012 3:11 pm
Subject: Re: [Python-ideas] Optional kwarg making attrgetter & itemgetter always return a tuple
On 9/13/2012 9:15 AM, Masklinn wrote:
> attrgetter and itemgetter are both very useful functions, but both have
This seems like a plausible idea. The actual C version requires one > a significant pitfall if the arguments passed in are validated but not > controlled: if receiving the arguments (list of attributes, keys or > indexes) from an external source and *-applying it, if the external > source passes a sequence of one element both functions will in turn > return an element rather than a singleton (1-element tuple). > This means such code, for instance code "slicing" a matrix of some sort
> slicer = (operator.itemgetter(*indices) if len(indices) > 1
> This makes for more verbose and less straightforward code, I think it
> # works the same no matter what len(indices) is
> which in the example equivalences[0] would be an override (to False) of
> The argument is backward-compatible as neither function currently
> Uncertainty note: whether force_tuple (or whatever its name is)
> [0] http://docs.python.org/dev/library/operator.html#operator.attrgetter
argument. The Python equivalent in the doc does not (hence the different signature), as it would return an empty tuple for empty *items. -- _______________________________________________
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||