Advantages of Default Factory in Dataclasses

Abdur-Rahmaan Janhangeer arj.python at gmail.com
Sun Nov 21 12:51:33 EST 2021


On Tue, Nov 16, 2021 at 7:17 PM Paul Bryan <pbryan at anode.ca> wrote:

> On Tue, 2021-11-16 at 17:04 +0400, Abdur-Rahmaan Janhangeer wrote:
>
> A simple question: why do we need field(default_factory ) in dataclasses?
>
>
> To initialize a default value when a new instance of the dataclass is
> created. For example, if you want a field to default to a dict. A new dict
> is created for each instance of the dataclass created.
>


Why not have an attribute which returns a deep copy of a dict?

Like the only advantage of default factory is copying whatever we specify?


More information about the Python-list mailing list