Skip to content

Consider allowing frozendicts to be assigned to instance dicts #145119

@rhettinger

Description

@rhettinger

Currently, can make an assignment to an instance's __dict__ attribute but the value must be an actual dict. It should allow frozendict as well.

This would be useful for making immutable instances without the awkward work arounds used in frozen dataclasses.

>>> class Point:
...     def __init__(self, x, y):
...         self.__dict__ = frozendict(x=x, y=y)
...
        
>>> p = Point(10, 20)
Traceback (most recent call last):
  File "<pyshell#53>", line 1, in <module>
    p = Point(10, 20)
  File "<pyshell#52>", line 3, in __init__
    self.__dict__ = frozendict(x=x, y=y)
TypeError: __dict__ must be set to a dictionary, not a 'frozendict'

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions