python / python/cpython

Add a `default` argument to `operator.attrgetter` and `operator.itemgetter`

Open
#129,305 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

extension-modules stdlib type-feature
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Feature or enhancement

Proposal:

The proposal if for these functions to receive a default argument.

The main motivation is operator.attrgetter, which implements nested attribute access, but is missing getattr's default value support. operator.itemgetter also gets the default argument for consistency.

For operator.attrgetter, only AttributeErrors should be suppressed, and for operator.itemgetter, KeyError.

Has this already been discussed elsewhere?

I have already discussed this feature proposal on Discourse

Links to previous discussion of this feature:

https://discuss.python.org/t/enhancing-getattr-to-support-nested-attribute-access-with-dotted-strings/74305/12

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the proposed behavior for operator.attrgetter and operator.itemgetter, then read the linked Discourse discussion for unresolved design questions. Trace the existing implementations and tests for both functions; done means supporting a default argument while suppressing only AttributeError for attrgetter and KeyError for itemgetter.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.