What does frozen mean for dataclasses?

We Are Going To Discuss About What does frozen mean for dataclasses?. So lets Start this Python Article.

What does frozen mean for dataclasses?

  1. How to solve What does frozen mean for dataclasses?

    In Python, “frozen” means an object cannot be modified. For example, consider set and frozenset:
    >>> s = set((1, 2, 3)) >>> s {1, 2, 3} >>> s.add(4) >>> s {1, 2, 3, 4} >>> fs = frozenset((1, 2, 3)) >>> fs frozenset({1, 2, 3}) >>> fs.add(4) ... AttributeError: 'frozenset' object has no attribute 'add'
    Likewise, creating a dataclass with frozen=True means its instances are frozen and cannot be changed.
    Be aware that frozen only applies to the dataclass instance itself – a frozen dataclass can contain mutable items such as lists, and a regular dataclass can contain frozen/immutable items such as tuples.

    The point of frozen objects is to avoid accidental modification, and to guarantee a consistent value.
    The former is advantageous to avoid bugs. When an object is not intended to be modified, making it frozen reveals accidental modification via an immediate error.
    The latter allows use as immutable object, for example the keys of a dict. A frozen dataclass is by default hashable and suitable as a dict key.
    from dataclasses import dataclass @dataclass(frozen=True) class Frozen: x: int y: int named_points = {Frozen(0, 0): "Origin"}
    Note that hashability does not just depend on the dataclass but is recursive – a frozen dataclass containing a list is not hashable, because the list is not hashable.

  2. What does frozen mean for dataclasses?

    In Python, “frozen” means an object cannot be modified. For example, consider set and frozenset:
    >>> s = set((1, 2, 3)) >>> s {1, 2, 3} >>> s.add(4) >>> s {1, 2, 3, 4} >>> fs = frozenset((1, 2, 3)) >>> fs frozenset({1, 2, 3}) >>> fs.add(4) ... AttributeError: 'frozenset' object has no attribute 'add'
    Likewise, creating a dataclass with frozen=True means its instances are frozen and cannot be changed.
    Be aware that frozen only applies to the dataclass instance itself – a frozen dataclass can contain mutable items such as lists, and a regular dataclass can contain frozen/immutable items such as tuples.

    The point of frozen objects is to avoid accidental modification, and to guarantee a consistent value.
    The former is advantageous to avoid bugs. When an object is not intended to be modified, making it frozen reveals accidental modification via an immediate error.
    The latter allows use as immutable object, for example the keys of a dict. A frozen dataclass is by default hashable and suitable as a dict key.
    from dataclasses import dataclass @dataclass(frozen=True) class Frozen: x: int y: int named_points = {Frozen(0, 0): "Origin"}
    Note that hashability does not just depend on the dataclass but is recursive – a frozen dataclass containing a list is not hashable, because the list is not hashable.

Solution 1

In Python, “frozen” means an object cannot be modified. For example, consider set and frozenset:

>>> s = set((1, 2, 3))
>>> s
{1, 2, 3}
>>> s.add(4)
>>> s
{1, 2, 3, 4}
>>> fs = frozenset((1, 2, 3))
>>> fs
frozenset({1, 2, 3})
>>> fs.add(4)
...
AttributeError: 'frozenset' object has no attribute 'add'

Likewise, creating a dataclass with frozen=True means its instances are frozen and cannot be changed.

Be aware that frozen only applies to the dataclass instance itself – a frozen dataclass can contain mutable items such as lists, and a regular dataclass can contain frozen/immutable items such as tuples.


The point of frozen objects is to avoid accidental modification, and to guarantee a consistent value.

  • The former is advantageous to avoid bugs. When an object is not intended to be modified, making it frozen reveals accidental modification via an immediate error.
  • The latter allows use as immutable object, for example the keys of a dict. A frozen dataclass is by default hashable and suitable as a dict key.
from dataclasses import dataclass

@dataclass(frozen=True)
class Frozen:
    x: int
    y: int

named_points = {Frozen(0, 0): "Origin"}

Note that hashability does not just depend on the dataclass but is recursive – a frozen dataclass containing a list is not hashable, because the list is not hashable.

Original Author MisterMiyagi Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

ittutorial team

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment