Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Optional kwarg making attrgetter & itemgetter always return a tuple
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Terry Reedy  
View profile  
 More options Sep 13 2012, 3:14 pm
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
> 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
> to get only some columns and getting the slicing information from its
> caller (in situation where extracting a single column may be perfectly
> sensible) will have to implement a manual dispatch between a "manual"
> getitem (or getattr) and an itemgetter (resp. attrgetter) call, e.g.

>      slicer = (operator.itemgetter(*indices) if len(indices) > 1
>                else lambda ar: [ar[indices[0]])

> This makes for more verbose and less straightforward code, I think it
> would be useful to such situations if attrgetter and itemgetter could be
> forced into always returning a tuple by way of an optional argument:

>      # works the same no matter what len(indices) is
>      slicer = operator.itemgetter(*indices, force_tuple=True)

> which in the example equivalences[0] would be an override (to False) of
> the `len` check (`len(items) == 1` would become `len(items) == 1 and not
> force_tuple`)

> The argument is backward-compatible as neither function currently
> accepts any keyword argument.

> Uncertainty note: whether force_tuple (or whatever its name is)
> silences the error generated when len(indices) == 0, and returns
> a null tuple rather than raising a TypeError.

> [0] http://docs.python.org/dev/library/operator.html#operator.attrgetter

This seems like a plausible idea. The actual C version requires one
argument. The Python equivalent in the doc does not (hence the different
signature), as it would return an empty tuple for empty *items.

--
Terry Jan Reedy

_______________________________________________
Python-ideas mailing list
Python-id...@python.org
http://mail.python.org/mailman/listinfo/python-ideas


 
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.