Proposal: Add update(**kwargs) method to types.SimpleNamespace · Issue #136140 · python/cpython · GitHub | Latest TMZ Celebrity News & Gossip | Watch TMZ Live
Skip to content

Proposal: Add update(**kwargs) method to types.SimpleNamespace #136140

Closed as not planned
@TomasCorreia2003

Description

@TomasCorreia2003

Feature or enhancement

Proposal:

types.SimpleNamespace is a lightweight object for grouping attributes. However, it lacks a built-in way to update multiple attributes at once using keyword arguments.

Currently, users must write:

ns.__dict__.update({'x': 1, 'y': 2})

This leaks implementation details and reduces code clarity. A built-in method would make this cleaner and more idiomatic.

Add an update(**kwargs) method to SimpleNamespace that updates the namespace in place:

from types import SimpleNamespace

ns = SimpleNamespace(a=1)
ns.update(b=2, c=3)
print(ns)  # namespace(a=1, b=2, c=3)
  • Implemented in Objects/namespaceobject.c

  • Calls PyDict_Update(self->dict, kwargs) directly

  • Uses the --\n\n signature string convention so inspect.signature() works

  • Added test in Lib/test/test_types.py

Benchmarks:
Measured over 1 million repetitions:

SimpleNamespace.update() | 1.25 s
dict.update() | 1.10 s
class().attr = value | 0.55 s

Testing:
Unit test confirms that update() modifies or adds attributes correctly

inspect.signature(SimpleNamespace.update) returns a valid signature

Full CPython test suite run — no regressions introduced

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/update-method-for-simplenamespace/97207

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      TMZ Celebrity News – Breaking Stories, Videos & Gossip

      Looking for the latest TMZ celebrity news? You've come to the right place. From shocking Hollywood scandals to exclusive videos, TMZ delivers it all in real time.

      Whether it’s a red carpet slip-up, a viral paparazzi moment, or a legal drama involving your favorite stars, TMZ news is always first to break the story. Stay in the loop with daily updates, insider tips, and jaw-dropping photos.

      🎥 Watch TMZ Live

      TMZ Live brings you daily celebrity news and interviews straight from the TMZ newsroom. Don’t miss a beat—watch now and see what’s trending in Hollywood.