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?
> >
> >
> >
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,
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
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
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=[]):
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
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