Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

<<<<< EVACUATION ROUTE <<<<<


devel / comp.lang.python / Re: Advantages of Default Factory in Dataclasses

SubjectAuthor
* Re: Advantages of Default Factory in DataclassesDavid Lowry-Duda
`* Re: Advantages of Default Factory in DataclassesAlan Bawden
 `- Re: Advantages of Default Factory in DataclassesDavid Lowry-Duda

1
Re: Advantages of Default Factory in Dataclasses

<mailman.28.1637104197.19479.python-list@python.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=20426&group=comp.lang.python#20426

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: david@lowryduda.com (David Lowry-Duda)
Newsgroups: comp.lang.python
Subject: Re: Advantages of Default Factory in Dataclasses
Date: Tue, 16 Nov 2021 18:00:05 -0500
Lines: 23
Message-ID: <mailman.28.1637104197.19479.python-list@python.org>
References: <CADrxXXksfD+nxVT0pPyHjX6jtkqfKDYvyTf11P7eT+Bbtbu81A@mail.gmail.com>
<YZQ39Zd0tEhhdEfL@icerm-dld>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
X-Trace: news.uni-berlin.de 9EyVk5WjJnxpohTg7xgzXgtXycTR3Xtmf0O7QmZOlM1Q==
Return-Path: <david@lowryduda.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="1024-bit key; unprotected key"
header.d=lowryduda.com header.i=@lowryduda.com header.b=WOsZQ997;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.090
X-Spam-Evidence: '*H*': 0.83; '*S*': 0.01; 'def': 0.04; 'prints':
0.09; 'question:': 0.09; 'attributes': 0.16; 'janhangeer': 0.16;
'wrote:': 0.16; '16,': 0.19; 'tue,': 0.19; 'to:addr:python-list':
0.20; 'code': 0.23; 'function': 0.27; 'expect': 0.28; "doesn't":
0.32; 'objects': 0.32; 'unexpected': 0.32; 'same': 0.34; 'header
:In-Reply-To:1': 0.34; 'following': 0.35; 'possibly': 0.36;
'people': 0.36; 'received:98': 0.40; 'to:': 0.62; 'upon': 0.64;
'lead': 0.67; '[1]': 0.67; '2021': 0.71; 'skip:f 20': 0.75;
'potentially': 0.76; 'attribute': 0.84; 'received:23.106': 0.84
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=lowryduda.com;
s=dkim; t=1637103608;
h=from:from:reply-to:subject:subject:date:date:message-id:message-id:
to:to:cc:mime-version:mime-version:content-type:content-type:
in-reply-to:in-reply-to:references:references;
bh=u6l/Yio+WbJntyiYFdtYlrst1O9jJqD/4z9MEhrqA/w=;
b=WOsZQ9974jPbLfPKM1lEWdmwYjd3/1hAcRa9q4pc3gtORsFeHAjPS8qUWHwwEN2FVsRVoc
trLMAmAooz7tkcZpvBTBf45joRjy7pPby+MMQbQprxklYV/v5cpGOMetWd5T3lN5Yyav6U
1I2wB4itICaebokLsIn1k+td/j0KpNo=
Mail-Followup-To: python-list@python.org
Content-Disposition: inline
In-Reply-To: <CADrxXXksfD+nxVT0pPyHjX6jtkqfKDYvyTf11P7eT+Bbtbu81A@mail.gmail.com>
X-Spam-Status: No, score=-1.01
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.37
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <YZQ39Zd0tEhhdEfL@icerm-dld>
X-Mailman-Original-References: <CADrxXXksfD+nxVT0pPyHjX6jtkqfKDYvyTf11P7eT+Bbtbu81A@mail.gmail.com>
 by: David Lowry-Duda - Tue, 16 Nov 2021 23:00 UTC

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

Re: Advantages of Default Factory in Dataclasses

<86lf1nhef8.fsf@williamsburg.bawden.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=20427&group=comp.lang.python#20427

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: alan@csail.mit.edu (Alan Bawden)
Newsgroups: comp.lang.python
Subject: Re: Advantages of Default Factory in Dataclasses
Date: Tue, 16 Nov 2021 18:24:43 -0500
Organization: ITS Preservation Society
Lines: 20
Message-ID: <86lf1nhef8.fsf@williamsburg.bawden.org>
References: <CADrxXXksfD+nxVT0pPyHjX6jtkqfKDYvyTf11P7eT+Bbtbu81A@mail.gmail.com>
<YZQ39Zd0tEhhdEfL@icerm-dld>
<mailman.28.1637104197.19479.python-list@python.org>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="004d97507d3c845004ba9522a3f1500b";
logging-data="20547"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18B05aeZmgVzfvhUxeRy8rs"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux)
Cancel-Lock: sha1:vzVQMscWZDaCElXKBIM10M3W1/w=
sha1:8qp3WOLdJ4egeOtJLEULC0omOXE=
 by: Alan Bawden - Tue, 16 Nov 2021 23:24 UTC

David Lowry-Duda <david@lowryduda.com> 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.)

Re: Advantages of Default Factory in Dataclasses

<mailman.33.1637250204.19479.python-list@python.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=20447&group=comp.lang.python#20447

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: david@lowryduda.com (David Lowry-Duda)
Newsgroups: comp.lang.python
Subject: Re: Advantages of Default Factory in Dataclasses
Date: Thu, 18 Nov 2021 10:43:10 -0500
Lines: 20
Message-ID: <mailman.33.1637250204.19479.python-list@python.org>
References: <CADrxXXksfD+nxVT0pPyHjX6jtkqfKDYvyTf11P7eT+Bbtbu81A@mail.gmail.com>
<YZQ39Zd0tEhhdEfL@icerm-dld>
<mailman.28.1637104197.19479.python-list@python.org>
<86lf1nhef8.fsf@williamsburg.bawden.org>
<YZZ0jr4oOZ0CGklg@icerm-dld>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
X-Trace: news.uni-berlin.de dLrBZQh07IBzv5yAmfUUbgytF4Vb6xnaecuoy5eNpLzQ==
Return-Path: <david@lowryduda.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="1024-bit key; unprotected key"
header.d=lowryduda.com header.i=@lowryduda.com header.b=PTJum9QS;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.129
X-Spam-Level: *
X-Spam-Evidence: '*H*': 0.78; '*S*': 0.04; 'def': 0.04; 'cc:addr
:python-list': 0.09; 'prints': 0.09; 'cc:no real name:2**0': 0.14;
'right.': 0.16; 'wrote:': 0.16; '16,': 0.19; 'tue,': 0.19;
'cc:addr:python.org': 0.20; 'cc:2**0': 0.25; "doesn't": 0.32;
'guess': 0.32; 'but': 0.32; 'header:In-Reply-To:1': 0.34; 'alan':
0.40; 'received:98': 0.40; 'both': 0.40; 'thus': 0.64; 'your':
0.64; '[1]': 0.67; '2021': 0.71; 'potentially': 0.76; 'quickly':
0.80; 'haha,': 0.84; 'people"': 0.84; 'received:23.106': 0.84
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=lowryduda.com;
s=dkim; t=1637250195;
h=from:from:reply-to:subject:subject:date:date:message-id:message-id:
to:to:cc:cc:mime-version:mime-version:content-type:content-type:
in-reply-to:in-reply-to:references:references;
bh=/gfYnGc35lg3iVq5rlAkyRdcf0wwLWopDedoxQ3ZQNY=;
b=PTJum9QS7w59pFzMK7Dbu4UPB9DcosdjG+81x3HzXGY5GXuKMW1K1JZLtbwXhDJ1JAuo96
a+pA1E8Fg3eNpt+J/OashCEImg+lVSftJZ2w0lpH0knzTOK/JFZtOjiepNO8MATLuv6UNh
rGInzRe/7m0cnADOtTb0u76V29njuTE=
Mail-Followup-To: Alan Bawden <alan@csail.mit.edu>, python-list@python.org
Content-Disposition: inline
In-Reply-To: <86lf1nhef8.fsf@williamsburg.bawden.org>
X-Spam-Status: No, score=-0.92
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.37
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <YZZ0jr4oOZ0CGklg@icerm-dld>
X-Mailman-Original-References: <CADrxXXksfD+nxVT0pPyHjX6jtkqfKDYvyTf11P7eT+Bbtbu81A@mail.gmail.com>
<YZQ39Zd0tEhhdEfL@icerm-dld>
<mailman.28.1637104197.19479.python-list@python.org>
<86lf1nhef8.fsf@williamsburg.bawden.org>
 by: David Lowry-Duda - Thu, 18 Nov 2021 15:43 UTC

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


devel / comp.lang.python / Re: Advantages of Default Factory in Dataclasses

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor