Re: Advantages of Default Factory in Dataclasses
On Sun, 2021-11-21 at 21:51 +0400, Abdur-Rahmaan Janhangeer wrote: > > On Tue, Nov 16, 2021 at 7:17 PM Paul Bryan 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
Re: Advantages of Default Factory in Dataclasses
On Tue, Nov 16, 2021 at 7:17 PM Paul Bryan 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? -- https://mail.python.org/mailman/listinfo/python-list
Re: Advantages of Default Factory in Dataclasses
On Tue, Nov 16, 2021 at 06:24:43PM -0500, Alan Bawden wrote: >```python >def add_to(elem, inlist=[]): >inlist.append(elem) >return inlist > >list1 = add_to(1) >list2 = add_to(2) >print(list1) # prints [1] >print(list2) # prints [1, 2], potentially confusing >``` > > Not only does it not print what "most people" expect. It also doesn't > print what _you_ expect! (But you made your point.) Haha, you're right. I would guess that I reordered the statements when I quickly checked this in the interpreter. But indeed, both list1 and list2 point to inlist, and thus are the same. Whoops! - DLD -- https://mail.python.org/mailman/listinfo/python-list
Re: Advantages of Default Factory in Dataclasses
David Lowry-Duda writes: ... For the same reason that the following code doesn't do what some people might expect it to: ```python def add_to(elem, inlist=[]): inlist.append(elem) return inlist list1 = add_to(1) list2 = add_to(2) print(list1) # prints [1] print(list2) # prints [1, 2], potentially confusing ``` Not only does it not print what "most people" expect. It also doesn't print what _you_ expect! (But you made your point.) -- https://mail.python.org/mailman/listinfo/python-list
Re: Advantages of Default Factory in Dataclasses
On Tue, Nov 16, 2021 at 05:04:05PM +0400, Abdur-Rahmaan Janhangeer wrote: > A simple question: why do we need field(default_factory ) in > dataclasses? For the same reason that the following code doesn't do what some people might expect it to: ```python def add_to(elem, inlist=[]): inlist.append(elem) return inlist list1 = add_to(1) list2 = add_to(2) print(list1) # prints [1] print(list2) # prints [1, 2], potentially confusing ``` Mutable attributes are created once, upon definition. Reusing the same function (or instantiating objects of the same class) and modifying this attribute can lead to possibly unexpected side effects as above. - DLD -- https://mail.python.org/mailman/listinfo/python-list
Re: Advantages of Default Factory in Dataclasses
On 17/11/2021 02.04, Abdur-Rahmaan Janhangeer wrote: > A simple question: why do we need field(default_factory ) in dataclasses? > > Why not make that field as an attribute return a function? > > Useful implementation examples / use cases appreciated. It's an interesting question: We could define a list (or whatever) within __post_init__()! Greater minds than mine may care to comment on the theory that something defined as a dataclasses.field will be allocated as part of the class constructor, ie lower-cost. Whereas, something defined post-init will require extra process-time and storage-allocation. (?) Doesn't it already return a (evaluated) function? Remember: one is not limited to Python built-in or PSL types. Thus: def countdown(): return [ 10, 9, 8, 7, ..., "blast-off" ] ... launch_commentary:list = field( default_factory=countdown ) The use of default-values for mutables is something of a Python 'gotcha'. My use-case is that some wise-soul's decision to do things this way prevents me from falling into that debugging "slough of despond". (does such qualify as a "use case"?) -- Regards, =dn -- https://mail.python.org/mailman/listinfo/python-list
Re: Advantages of Default Factory in Dataclasses
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 make that field as an attribute return a function? I do not understand the question. > Useful implementation examples / use cases appreciated. Any case where a you want a dataclass field to default to a mutable value. Examples: dicts, lists, other dataclasses. Paul -- https://mail.python.org/mailman/listinfo/python-list