Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow operator 'getter' methods to take a list and return a tuple #60661

Closed
bitdancer opened this issue Nov 12, 2012 · 4 comments
Closed

Allow operator 'getter' methods to take a list and return a tuple #60661

bitdancer opened this issue Nov 12, 2012 · 4 comments
Assignees
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@bitdancer
Copy link
Member

BPO 16457
Nosy @rhettinger, @ncoghlan, @bitdancer

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = 'https://github.com/rhettinger'
closed_at = <Date 2012-11-13.15:14:30.520>
created_at = <Date 2012-11-12.00:17:38.997>
labels = ['type-feature', 'library']
title = "Allow operator 'getter' methods to take a list and return a tuple"
updated_at = <Date 2012-11-13.15:14:30.518>
user = 'https://github.com/bitdancer'

bugs.python.org fields:

activity = <Date 2012-11-13.15:14:30.518>
actor = 'r.david.murray'
assignee = 'rhettinger'
closed = True
closed_date = <Date 2012-11-13.15:14:30.520>
closer = 'r.david.murray'
components = ['Library (Lib)']
creation = <Date 2012-11-12.00:17:38.997>
creator = 'r.david.murray'
dependencies = []
files = []
hgrepos = []
issue_num = 16457
keywords = []
message_count = 4.0
messages = ['175412', '175415', '175486', '175500']
nosy_count = 3.0
nosy_names = ['rhettinger', 'ncoghlan', 'r.david.murray']
pr_nums = []
priority = 'normal'
resolution = 'rejected'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue16457'
versions = ['Python 3.4']

@bitdancer
Copy link
Member Author

Consider this code snippet, simplified from a real application:

def display(self, *columns)
    getter = attrgetter(*columns)
    toprint = [[str(x) for x in getter(row)] for row in self._rows]

This works great...as long as there are two or more columns to print.
If there is only one column, it bombs because the getter returns a value
instead of a tuple.

This would not be a problem in and of itself, but there is no way to
tell attrgetter that I want a tuple even if there is only one value.

I believe it would be backward compatible to allow:

    attrgetter(['a'])
    itemgetter(['a', 'b'])

to return a tuple, since a list cannot be an attribute name. The same
would apply to itemgetter, since a list cannot be a dictionary key.

@bitdancer bitdancer added stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Nov 12, 2012
@ncoghlan
Copy link
Contributor

Lists can't be dictionary keys, but they can appear in other mappings (e.g. an id-keyed mapping). So, while you could add this capability to attrgetter without breaking backwards compatibility, it's not possible for itemgetter.

@rhettinger rhettinger self-assigned this Nov 13, 2012
@rhettinger
Copy link
Contributor

It's unfortunate that the automatic scalar/tuple switchover design doesn't play well with start-args. We have the same issue arising in a number of places (for example, min(*args) isn't happy when args is of length 1).

While inconvenient for variable length argument lists, I don't think the proposed solution is clean. As it currently stands, the signature is reasonably simple, easy to explain, and doesn't depend on exact type checks (like %-formatting does with tuple/dict arguments).

Instead of contorting the signature for itemgetter(), it would be better if the use case were to be addressed with the existing language features:

>>> t = tuple('abcdefghi')
>>> for columns in ([], [2], [2,4], [2,4,6]):
	print(tuple(t[i] for i in columns))
	
()
('c',)
('c', 'e')
('c', 'e', 'g')

@bitdancer
Copy link
Member Author

Given Nick's point about itemgetter, I agree this isn't worth doing. I wouldn't want the signatures of attrgetter and itemgetter to no longer be parallel.

Min isn't a problem, by the way, since it accepts an iterator as a single argument.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

3 participants