On Sun, 2021-11-21 at 21:51 +0400, Abdur-Rahmaan Janhangeer wrote:
> 
> On Tue, Nov 16, 2021 at 7:17 PM Paul Bryan <pbr...@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?

You can certainly write a default factory to return a deep copy. I'm
not understanding your question about the attribute though. Attribute
in what object? What might the code look like using an attribute?

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

The advantage of the default factory is that it can generate a value at
the time a data class is initialized.

-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to