Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

Life. Don't talk to me about life. -- Marvin the Paranoid Android


devel / comp.lang.python / Re: Configuring an object via a dictionary

SubjectAuthor
* Configuring an object via a dictionaryLoris Bennett
+- Re: Configuring an object via a dictionaryMats Wichmann
+- Re: Configuring an object via a dictionaryThomas Passin
+* Re: Configuring an object via a dictionaryTobiah
|`* Re: Configuring an object via a dictionaryTobiah
| `* Re: Configuring an object via a dictionaryLoris Bennett
|  `- Re: Configuring an object via a dictionaryPokemon Chw
+- Re: Configuring an object via a dictionaryGrant Edwards
+- Re: Configuring an object via a dictionaryThomas Passin
+- Re: Configuring an object via a dictionary (Posting On Python-List Prohibited)Lawrence D'Oliveiro
+- Re: Configuring an object via a dictionary2QdxY4RzWzUUiLuE
+- Re: Configuring an object via a dictionarydn
+- Re: Configuring an object via a dictionaryThomas Passin
+- RE: Configuring an object via a dictionary<avi.e.gross
`- Re: Configuring an object via a dictionaryAnders Munch

1
Configuring an object via a dictionary

<87y1ajdeqs.fsf@zedat.fu-berlin.de>

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: loris.bennett@fu-berlin.de (Loris Bennett)
Newsgroups: comp.lang.python
Subject: Configuring an object via a dictionary
Date: Fri, 15 Mar 2024 10:30:03 +0100
Organization: FUB-IT (ex-ZEDAT), Freie Universität Berlin
Lines: 39
Message-ID: <87y1ajdeqs.fsf@zedat.fu-berlin.de>
Mime-Version: 1.0
Content-Type: text/plain
X-Trace: news.uni-berlin.de xLxv2e5aRg5XhBdgeCzZ6gyOwYrsmktIMNqqWWsLGCx7Hk
Cancel-Lock: sha1:ojjJkhALhnIHVFHqJlrVMpcAeCo= sha1:R6XEBRZSlS9AblkymAhBv63Ko1c= sha256:dcufB/THs0/DLfRdDM90stLaWngShvykfvoT5oWL1bw=
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux)
 by: Loris Bennett - Fri, 15 Mar 2024 09:30 UTC

Hi,

I am initialising an object via the following:

def __init__(self, config):

self.connection = None

self.source_name = config['source_name']
self.server_host = config['server_host']
self.server_port = config['server_port']
self.user_base = config['user_base']
self.user_identifier = config['user_identifier']
self.group_base = config['group_base']
self.group_identifier = config['group_identifier']
self.owner_base = config['owner_base']

However, some entries in the configuration might be missing. What is
the best way of dealing with this?

I could of course simply test each element of the dictionary before
trying to use. I could also just write

self.config = config

but then addressing the elements will add more clutter to the code.

However, with a view to asking forgiveness rather than
permission, is there some simple way just to assign the dictionary
elements which do in fact exist to self-variables?

Or should I be doing this completely differently?

Cheers,

Loris

--
This signature is currently under constuction.

Re: Configuring an object via a dictionary

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: mats@wichmann.us (Mats Wichmann)
Newsgroups: comp.lang.python
Subject: Re: Configuring an object via a dictionary
Date: Fri, 15 Mar 2024 12:35:26 -0600
Lines: 12
Message-ID: <mailman.93.1710527741.3452.python-list@python.org>
References: <87y1ajdeqs.fsf@zedat.fu-berlin.de>
<fce120cd-214d-4220-8a1f-2b770ea7ac68@wichmann.us>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: news.uni-berlin.de MLDcM7uOxpnCoQPEJ1U8qwtN1n/CvfZ3cEDOtLkSCHaA==
Cancel-Lock: sha1:J69ii/y71uS0E0FcHjzHw/PLHFM= sha256:VD1fAd2IgiD0QGHNwrjiEzO/Yg0GGRkP6ekJqB3Bs4Q=
Return-Path: <mats@wichmann.us>
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=pobox.com header.i=@pobox.com header.b=J6PhQz87;
dkim-adsp=none (unprotected policy); dkim-atps=neutral
X-Spam-Status: OK 0.070
X-Spam-Evidence: '*H*': 0.87; '*S*': 0.01; 'received:173': 0.13;
'bennett': 0.16; 'wrote:': 0.16; 'to:addr:python-list': 0.20;
'object': 0.26; 'header:User-Agent:1': 0.30; 'python-list': 0.32;
'header:In-Reply-To:1': 0.34; 'received:192.168': 0.37;
'following:': 0.69
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed; d=pobox.com; h=message-id
:date:mime-version:subject:to:references:from:in-reply-to
:content-type:content-transfer-encoding; s=sasl; bh=TKX91pPY4N5X
Lc0kYQfgQ7+FGQBbljm6mj/TD7bEIKk=; b=J6PhQz87dT1ZM9SFBU9sjetgHaXD
ak/Kj3kitqc+GyMiQ7rNWuMUXDNMCNHrwQ4lJeg38R0GbRZMlVHWkAJmU5JQfwBZ
cmsLthUT3ZH01ydX4f6r8JXQyhNnEgbeaK9pLrK/lCaXFgwm2AvZ3uLs63tK1ui4
0jTZ9ceGMu4/PDQ=
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed; d=wichmann.us;
h=message-id:date:mime-version:subject:to:references:from:in-reply-to:content-type:content-transfer-encoding;
s=2018-07.pbsmtp; bh=TKX91pPY4N5XLc0kYQfgQ7+FGQBbljm6mj/TD7bEIKk=;
b=BhMx/+zF9/2Jm0TAZHA7qe3aMsIrTGIPXkSXUKhnLXvJMat4qIhuCAV0aoWG09sh7N8vDfG65MENJVScA8FiRswgvu6LZ/kmZ8C4TSEtAEEnh4WfYHywceTF7EmU0CZGX8S4i126w/YTMQCqKrZnYT/LFyxpDR2KY0UNiPp/5PE=
User-Agent: Mozilla Thunderbird
Content-Language: en-US
Autocrypt: addr=mats@wichmann.us; keydata=
xsDiBD9xp6oRBAC1vd3YI8Gcr1CxpV1gldNQu0uQsNaICDk+Ai3+R163s/P83JOYG+SBEA3P
v7iZx70qpQ3RzP7KrjF1Nm6j0em9ccUX2fPQUCAxXw5Hiq7CSMiwQQZRI6shcnyMh9XTKViT
WK5MrKDyvjDEn7epjKzKwPS5SG039l6XaOKU0A4uGwCgsNqUQqC0gMMcbKlJV8ql58iKmbMD
/ii8FPQrXmyS/FnsPs7UddV5qMHKm7NUH5oiKuMVyakInRyq9iIxuu3D4Ec6mWRKcGsjmIkW
HXCSz0aefs6dsqNqpU54cYioJ3wP5LzHK7oclgJPryVt5Qezbdutf8SQf8gVkaNIlkxwGUzi
bKTZ6CHzwlz9nNgeel0XPUcZzFxGA/4paeCg2rMSVuAhUQbsLYHu4XzTs9P16zaXkrtxc4m5
b+BF5xsLgTpyO5l859XudS2Gp+7/Y37dAU4QlyGGOboWmF1y9U5DnzBwG8ghsnym+ga58MJh
LdRdQQ6xQolCpEXOuzm40f2r5uMxF3KOJ7WpIPuGAkeCPru9BmlATH+zOs0gTWF0cyBXaWNo
bWFubiA8bWF0c0B3aWNobWFubi51cz7CYQQTEQIAIQIbAwYLCQgHAwIDFQIDAxYCAQIeAQIX
gAUCT0VyZwIZAQAKCRDAMaCQc9hUxiZBAJ9cWziGp7hVfsu5T+cQptc3rLNndQCgrZh8u5LW
BfJ5e/Y+3PwZ8UEm+ELOwE0EP5is8BAEAMtwzcA8TYf5UTjDMgwcSNoErTc9ag+IX05QFgL8
aF8sfJRv5atcitqQy0gSIsOzI+L/AFdPN/+QQI3dL1tCq14t32KPDtigDhzm6jVPXX5z+V9u
xnD8XTp+ZvNcWoHXjViM8aXeLLEiCpiVCho307h3XShvqoKINWRQWeAsKKDDAAMFA/48zaey
wiiEyvI0meJ1KkNHxdLP0yLODr1WV6j9xkPkLWOaIDw7dlwEOlF1N1YtZ2wa0p1wsttdIbIx
ffgwXmcH4zrdxUIMz3U0BqYzk5H+5cYFXECXTFVOmweS+JECYMj80PjRoKCO1eVO1N30zksB
36NnhZWPRWIhjK3ZarIYH8JGBBgRAgAGBQI/mKzwAAoJEMAxoJBz2FTG6VEAoKDYHfDp5Q3q
PuPvPahCE9HsXMgAAJ9INTqcLSJrOfyJ8q95nBO1T26H2Q==
In-Reply-To: <87y1ajdeqs.fsf@zedat.fu-berlin.de>
X-Pobox-Relay-ID: CBD02B9C-E2FA-11EE-B141-A19503B9AAD1-81526775!pb-smtp21.pobox.com
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
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: <fce120cd-214d-4220-8a1f-2b770ea7ac68@wichmann.us>
X-Mailman-Original-References: <87y1ajdeqs.fsf@zedat.fu-berlin.de>
 by: Mats Wichmann - Fri, 15 Mar 2024 18:35 UTC

On 3/15/24 03:30, Loris Bennett via Python-list wrote:
> Hi,
>
> I am initialising an object via the following:

> self.source_name = config['source_name']

config.get('source_name', default_if_not_defined) is a common technique...

Re: Configuring an object via a dictionary

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: list1@tompassin.net (Thomas Passin)
Newsgroups: comp.lang.python
Subject: Re: Configuring an object via a dictionary
Date: Fri, 15 Mar 2024 14:45:21 -0400
Lines: 42
Message-ID: <mailman.94.1710528334.3452.python-list@python.org>
References: <87y1ajdeqs.fsf@zedat.fu-berlin.de>
<3cbc94a7-bf8e-4ee7-ae70-ba8b73ec61bd@tompassin.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: news.uni-berlin.de FQuYoIXflq9kmD7LaZwZbAkMqzMCdD40Zc2ZHvEzqKtw==
Cancel-Lock: sha1:U4EgjDEHt6KRHy6F+WY6+6aFChk= sha256:FIIJSGnFH9Gop0edDae4mBwEIOJ1FCqdFJThoE0RDy4=
Return-Path: <list1@tompassin.net>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=tompassin.net header.i=@tompassin.net header.b=rTntyFXY;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.017
X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'def': 0.04; 'better,':
0.09; 'expression': 0.09; 'bennett': 0.16; 'received:10.0.0':
0.16; 'received:64.90': 0.16; 'received:64.90.62': 0.16;
'received:64.90.62.162': 0.16; 'received:dreamhost.com': 0.16;
'specify': 0.16; 'wrote:': 0.16; 'code.': 0.17; 'to:addr:python-
list': 0.20; 'skip:_ 10': 0.22; 'object': 0.26; 'fact': 0.28;
'this?': 0.29; 'header:User-Agent:1': 0.30; 'am,': 0.31;
'default': 0.31; "doesn't": 0.32; 'elements': 0.32; 'python-list':
0.32; 'received:10.0': 0.32; 'received:mailchannels.net': 0.32;
'received:relay.mailchannels.net': 0.32; 'but': 0.32; 'there':
0.33; 'header:In-Reply-To:1': 0.34; 'trying': 0.35; 'one.': 0.35;
'way': 0.38; 'could': 0.38; 'should': 0.40; 'view': 0.60; 'best':
0.61; 'simply': 0.63; 'key': 0.64; 'header:Received:6': 0.67;
'received:64': 0.67; 'following:': 0.69
X-Sender-Id: dreamhost|x-authsender|tpassin@tompassin.net
ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1710528323; a=rsa-sha256;
cv=none;
b=dqdBswQ6Jnly6nkPjA9vhOjK3K6WOBsOdYeF9c6r15tMgyi1vua6OSujpPlUnOvEfYGHP5
9Kl2I6vQMZiJ6ulW9GzZgVAmnp5+rwOkqhzp0PnxIi/g2yTHmuOmz44HuLIQDUpPLEIVo5
ZgiVEwFEOktDiXZdzbIR/uOlYAtVn6GuYwRuhqZfvjHDnGDgTyH9KCIrZPsMmhLcBpLnp+
j+fuAOZckA4vmAU5+0TBlJv0jpfww7MsAYuKgty7nPANXaNpm9+PAFiKYf7kDm8FrSz2hj
WDfMgYrf1QGPhA1zOBmvA229gKRVel/+5EnZY+7V1EBItgwuIjiXu5SnQG3axw==
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed;
d=mailchannels.net; s=arc-2022; t=1710528323;
h=from:from:reply-to:subject:subject:date:date:message-id:message-id:
to:to:cc:mime-version:mime-version:content-type:content-type:
content-transfer-encoding:content-transfer-encoding:
in-reply-to:in-reply-to:references:references:dkim-signature;
bh=WxEFph+SyCPC9NJ1Dq5SpuS5DxHH7bmPBIXcIWTEkJ8=;
b=6MEkJSGFc2b5KYBgK/bVVKyxOYGgzJG//RwjHd4jeFB8ljVOUSPPFqTZmbJiebjeldyx2H
gRlVZFmu5T9zIdGNmPr+9nuMl4wOQZIJix+vcjoKJjRRxYazDojwTIsjs4dwSzbHSmTe2e
PYU4Lj1hYNqi/yII+skdtqW/FEdUv2hOSoCs04loJnN9H15xM0PuEa8U2Nx938sQAdPhpR
D/EpDNsqMv4mx6BO5mIQiW+hhLAC2pHrSZ+Vty/cC76QjpXtot7IoRW6U9TbZv2FdGXAhZ
7eKCgExVF1IZdBPOoMgIk3vgYGms7WpCT6wJQlG0g9GFlJvzzoKDHOrQ0vJmQw==
ARC-Authentication-Results: i=1; rspamd-6fd6c7ddc4-v2vkp;
auth=pass smtp.auth=dreamhost smtp.mailfrom=list1@tompassin.net
X-Sender-Id: dreamhost|x-authsender|tpassin@tompassin.net
X-MC-Relay: Neutral
X-MailChannels-SenderId: dreamhost|x-authsender|tpassin@tompassin.net
X-MailChannels-Auth-Id: dreamhost
X-Cooing-Battle: 32bd740c03f6c769_1710528323309_395339382
X-MC-Loop-Signature: 1710528323309:2848646099
X-MC-Ingress-Time: 1710528323308
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tompassin.net;
s=dreamhost; t=1710528322;
bh=bgu3TX9UZcX8WIvawmvkIlKBnZySwEanhdGst0rzd0Q=;
h=Date:Subject:To:From:Content-Type:Content-Transfer-Encoding;
b=rTntyFXY6CDsEH+HrI1RLysFt5h0mZnmJjCsBHLh4wIpPu3AVMxxnYJh83zRkc/oC
u78WdR+ru2XroWy138mF7GvIbqDc1FZSYI51tSc7EUjWr4IfPd2+Ym3s5UtBgZEbb2
w9uyco1vdsw8d7cpKGC2h9GeJapp3gS5fVY07L44GUZEhMyfPhgoVWs0Hw6UutB1AS
aedGIVE3BGZDSRjJQu6wOcdd1fBYkEkeatrqcQWCLXKqZR4eW+XYlzbF9fTmRkR3u0
R2fGzcrcOTeHVVl5BW+uFm7dCCM5rjWvtFyUMM/wYgWm8ObgcflMoCzYinNarFQ8k6
3iaPP5Fj+VTVg==
User-Agent: Mozilla Thunderbird
Content-Language: en-US
In-Reply-To: <87y1ajdeqs.fsf@zedat.fu-berlin.de>
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
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: <3cbc94a7-bf8e-4ee7-ae70-ba8b73ec61bd@tompassin.net>
X-Mailman-Original-References: <87y1ajdeqs.fsf@zedat.fu-berlin.de>
 by: Thomas Passin - Fri, 15 Mar 2024 18:45 UTC

On 3/15/2024 5:30 AM, Loris Bennett via Python-list wrote:
> Hi,
>
> I am initialising an object via the following:
>
> def __init__(self, config):
>
> self.connection = None
>
> self.source_name = config['source_name']
> self.server_host = config['server_host']
> self.server_port = config['server_port']
> self.user_base = config['user_base']
> self.user_identifier = config['user_identifier']
> self.group_base = config['group_base']
> self.group_identifier = config['group_identifier']
> self.owner_base = config['owner_base']
>
> However, some entries in the configuration might be missing. What is
> the best way of dealing with this?
>
> I could of course simply test each element of the dictionary before
> trying to use. I could also just write
>
> self.config = config
>
> but then addressing the elements will add more clutter to the code.
>
> However, with a view to asking forgiveness rather than
> permission, is there some simple way just to assign the dictionary
> elements which do in fact exist to self-variables?
>
> Or should I be doing this completely differently?

self.source_name = config.get('source_name', default_value)

Or, if you like this kind of expression better,

self.source_name = config.get('source_name') or default_value

..get() will return None if the key doesn't exist, or the default value
if you specify one.

Re: Configuring an object via a dictionary

<ut25q8$2dtqs$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: toby@tobiah.org (Tobiah)
Newsgroups: comp.lang.python
Subject: Re: Configuring an object via a dictionary
Date: Fri, 15 Mar 2024 11:59:20 -0700
Organization: A noiseless patient Spider
Lines: 29
Message-ID: <ut25q8$2dtqs$1@dont-email.me>
References: <87y1ajdeqs.fsf@zedat.fu-berlin.de>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 15 Mar 2024 18:59:20 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="a347ba70ab9fe10d00899cbebfb6fec1";
logging-data="2553692"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19H8epEVE9gKDplR8vZRKWt"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:rmhRjrCSGbR+k77+Ewlqg/ABAfo=
Content-Language: en-US
In-Reply-To: <87y1ajdeqs.fsf@zedat.fu-berlin.de>
 by: Tobiah - Fri, 15 Mar 2024 18:59 UTC

On 3/15/24 02:30, Loris Bennett wrote:
> Hi,
>
> I am initialising an object via the following:
>
> def __init__(self, config):
>
> self.connection = None
>
> self.source_name = config['source_name']
> self.server_host = config['server_host']

> However, with a view to asking forgiveness rather than
> permission, is there some simple way just to assign the dictionary
> elements which do in fact exist to self-variables?

class Foo():

def __init__(self, config):

for key, val in config.iteritems():
setattr(self, key, val)

f = Foo({'cat': 'dog'})

print(f.cat)

(outputs 'dog')

Re: Configuring an object via a dictionary

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: grant.b.edwards@gmail.com (Grant Edwards)
Newsgroups: comp.lang.python
Subject: Re: Configuring an object via a dictionary
Date: Fri, 15 Mar 2024 15:09:43 -0400 (EDT)
Lines: 51
Message-ID: <mailman.95.1710529784.3452.python-list@python.org>
References: <87y1ajdeqs.fsf@zedat.fu-berlin.de>
<3cbc94a7-bf8e-4ee7-ae70-ba8b73ec61bd@tompassin.net>
<4TxDMW3Dn1znVFX@mail.python.org>
X-Trace: news.uni-berlin.de F0wItLq7oEoeqd+fax1ncwHOY9CUlJy1otwlSIC0EWXA==
Cancel-Lock: sha1:kh2/5wsmfd+YoqtTefvRy6fHGGQ= sha256:FEI50Ez5Q9AKFmA2ChG2krGIqxuGObucui3WyPBUBGs=
Return-Path: <grant.b.edwards@gmail.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=none reason="no signature";
dkim-adsp=none (unprotected policy); dkim-atps=neutral
X-Spam-Status: OK 0.016
X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'def': 0.04; '(e.g.': 0.05;
'better,': 0.09; 'expression': 0.09; 'bennett': 0.16;
'from:addr:grant.b.edwards': 0.16; 'from:name:grant edwards':
0.16; 'wrote:': 0.16; 'code.': 0.17; 'to:addr:python-list': 0.20;
'skip:_ 10': 0.22; 'version': 0.23; 'object': 0.26; '>>>': 0.28;
'fact': 0.28; 'this?': 0.29; 'header:User-Agent:1': 0.30; 'am,':
0.31; '"",': 0.32; 'elements': 0.32; 'python-list': 0.32; 'but':
0.32; 'there': 0.33; 'trying': 0.35; 'from:addr:gmail.com': 0.35;
'way': 0.38; 'could': 0.38; 'should': 0.40; 'view': 0.60; 'best':
0.61; 'simply': 0.63; 'message-id:invalid': 0.68; 'following:':
0.69; 'latter': 0.69
User-Agent: slrn/1.0.3 (Linux)
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
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: <4TxDMW3Dn1znVFX@mail.python.org>
X-Mailman-Original-References: <87y1ajdeqs.fsf@zedat.fu-berlin.de>
<3cbc94a7-bf8e-4ee7-ae70-ba8b73ec61bd@tompassin.net>
 by: Grant Edwards - Fri, 15 Mar 2024 19:09 UTC

On 2024-03-15, Thomas Passin via Python-list <python-list@python.org> wrote:
> On 3/15/2024 5:30 AM, Loris Bennett via Python-list wrote:
>> Hi,
>>
>> I am initialising an object via the following:
>>
>> def __init__(self, config):
>>
>> self.connection = None
>>
>> self.source_name = config['source_name']
>> self.server_host = config['server_host']
>> self.server_port = config['server_port']
>> self.user_base = config['user_base']
>> self.user_identifier = config['user_identifier']
>> self.group_base = config['group_base']
>> self.group_identifier = config['group_identifier']
>> self.owner_base = config['owner_base']
>>
>> However, some entries in the configuration might be missing. What is
>> the best way of dealing with this?
>>
>> I could of course simply test each element of the dictionary before
>> trying to use. I could also just write
>>
>> self.config = config
>>
>> but then addressing the elements will add more clutter to the code.
>>
>> However, with a view to asking forgiveness rather than
>> permission, is there some simple way just to assign the dictionary
>> elements which do in fact exist to self-variables?
>>
>> Or should I be doing this completely differently?
>
> self.source_name = config.get('source_name', default_value)
>
> Or, if you like this kind of expression better,
>
> self.source_name = config.get('source_name') or default_value

Won't the latter version misbehave if the value of config['source_name'] has a
"false" boolean value (e.g. "", 0, 0.0, None, [], (), {}, ...)

>>> config = {}
>>> config['source_name'] = ""
>>> config.get('source_name') or 'default'
'default'

Re: Configuring an object via a dictionary

<ut275n$2e6lj$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: toby@tobiah.org (Tobiah)
Newsgroups: comp.lang.python
Subject: Re: Configuring an object via a dictionary
Date: Fri, 15 Mar 2024 12:22:30 -0700
Organization: A noiseless patient Spider
Lines: 13
Message-ID: <ut275n$2e6lj$1@dont-email.me>
References: <87y1ajdeqs.fsf@zedat.fu-berlin.de> <ut25q8$2dtqs$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 15 Mar 2024 19:22:31 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="a347ba70ab9fe10d00899cbebfb6fec1";
logging-data="2562739"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+FVp0ioqwodG2zZc41ICww"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:kWWCFL8giJ0a27WmR7o1Y0aSZI4=
In-Reply-To: <ut25q8$2dtqs$1@dont-email.me>
Content-Language: en-US
 by: Tobiah - Fri, 15 Mar 2024 19:22 UTC

I should mention that I wanted to answer your question,
but I wouldn't actually do this. I'd rather opt for
your self.config = config solution. The config options
should have their own namespace.

I don't mind at all referencing foo.config['option'],
or you could make foo.config an object by itself so
you can do foo.config.option. You'd fill it's attributes
in the same way I suggested for your main object.

Re: Configuring an object via a dictionary

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!rocksolid2!news.neodome.net!fu-berlin.de!uni-berlin.de!not-for-mail
From: list1@tompassin.net (Thomas Passin)
Newsgroups: comp.lang.python
Subject: Re: Configuring an object via a dictionary
Date: Fri, 15 Mar 2024 15:48:17 -0400
Lines: 58
Message-ID: <mailman.96.1710532109.3452.python-list@python.org>
References: <87y1ajdeqs.fsf@zedat.fu-berlin.de>
<3cbc94a7-bf8e-4ee7-ae70-ba8b73ec61bd@tompassin.net>
<4TxDMW3Dn1znVFX@mail.python.org>
<b169e599-e81f-45ce-8e2a-7027b59a4627@tompassin.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: news.uni-berlin.de ibWrgkxZORcAOovYmZ0DEQD1HlpH9UWh1J5+zsr8vL9A==
Cancel-Lock: sha1:LEziOiZEs+vYZq2mo0mG/xHte+4= sha256:cXH+VUsq2zcKPFgTcvzcxzjyReoE5aUnswCi9t0DJrA=
Return-Path: <list1@tompassin.net>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=tompassin.net header.i=@tompassin.net header.b=kwKsHNjr;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.004
X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'def': 0.04; '(e.g.': 0.05;
'better,': 0.09; 'expression': 0.09; 'received:23.83.212': 0.09;
'received:elm.relay.mailchannels.net': 0.09; '>>>>': 0.16;
'anyway.': 0.16; 'bennett': 0.16; 'oh,': 0.16; 'received:10.0.0':
0.16; 'received:64.90': 0.16; 'received:64.90.62': 0.16;
'received:64.90.62.162': 0.16; 'received:dreamhost.com': 0.16;
'right.': 0.16; 'wrote:': 0.16; 'values': 0.17; 'code.': 0.17;
'grant': 0.17; 'probably': 0.17; 'pm,': 0.19; 'to:addr:python-
list': 0.20; "i've": 0.22; 'skip:_ 10': 0.22; 'version': 0.23;
'code': 0.23; 'object': 0.26; 'bit': 0.27; '>>>': 0.28; 'expect':
0.28; 'fact': 0.28; 'this?': 0.29; 'header:User-Agent:1': 0.30;
'am,': 0.31; 'default': 0.31; 'putting': 0.31; '"",': 0.32;
'elements': 0.32; 'gotten': 0.32; 'python-list': 0.32;
'received:10.0': 0.32; 'received:mailchannels.net': 0.32;
'received:relay.mailchannels.net': 0.32; 'but': 0.32; 'there':
0.33; 'header:In-Reply-To:1': 0.34; 'trying': 0.35; 'really':
0.37; 'using': 0.37; "it's": 0.37; 'way': 0.38; 'could': 0.38;
'should': 0.40; 'view': 0.60; 'best': 0.61; 'simply': 0.63;
'header:Received:6': 0.67; 'received:64': 0.67; 'following:':
0.69; 'latter': 0.69; 'risk': 0.71; 'future': 0.72; 'low': 0.74;
'clause': 0.84; 'sometime': 0.91
X-Sender-Id: dreamhost|x-authsender|tpassin@tompassin.net
ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1710532098; a=rsa-sha256;
cv=none;
b=D2x8O3hi0jAfrbcLmD2smmIBdgG/EWhPY3Z5wcwUbFQhenNvggsGGt574WMtHD87C29IDx
ao/j8251mrmK4eV/XgbDb0opu1XwKR9VG+I7uA2QLZTb6P2rsxW4SF4f0mJpOZ/zdJBoYJ
GGGXuiSj345VXgeXgsgrxlhqS9tzFRaiS8PG5bAXWOjisN1Sms1smYZvyqCWhjWuZenfCl
nhwVZD3Biw0aLnFl1n77gGwRMHagmTM4n93R7Aqlf1NVYzui+xTikUpAm3d28VSMbHne1m
dK2GBu7WzLPnBHyMWt9vIZ2RoN6rWcLaYWN++zMK8iY3QIBQqSBrk8xkQ9GD1g==
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed;
d=mailchannels.net; s=arc-2022; t=1710532098;
h=from:from:reply-to:subject:subject:date:date:message-id:message-id:
to:to:cc:mime-version:mime-version:content-type:content-type:
content-transfer-encoding:content-transfer-encoding:
in-reply-to:in-reply-to:references:references:dkim-signature;
bh=kfQfePROgyWR4EEyH9fyVwuIzNipDKI0dR3XgudkTFs=;
b=8gtQAAMok4tsbrU2QYap2EEKpxvlji1gNHHOzyuYXXQkzVfeguWTKd2V1gdQL+XAM594xI
kX+4bI5h3h3WM8cuHuHQxM5aCv6Hd7+F5EkK7oCqajFkbfv/gAVgU08JHHjSwCDwmtiNx/
i2W9MWMd/my44/T7LfJKEE5olsXNIqFzeeNuBngB7M90LPuE+9dD0Cla2+49iKUna2/3AZ
fkTudL5NXxihjyIMyReciJ7BwnOfkFQmkEasmk7TmNoFIeR7AXgYLbdGTDslu+ZsAOecIG
FoUc8lmZpm2m0a3Q6LPFBF58wPpBLujn6pHw6675GkpKNUa1/4y/f8eizvT5nQ==
ARC-Authentication-Results: i=1; rspamd-57f75cb955-pbvbq;
auth=pass smtp.auth=dreamhost smtp.mailfrom=list1@tompassin.net
X-Sender-Id: dreamhost|x-authsender|tpassin@tompassin.net
X-MC-Relay: Neutral
X-MailChannels-SenderId: dreamhost|x-authsender|tpassin@tompassin.net
X-MailChannels-Auth-Id: dreamhost
X-Spot-Keen: 6cda3556031e2402_1710532098943_2687885813
X-MC-Loop-Signature: 1710532098943:3316269359
X-MC-Ingress-Time: 1710532098943
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tompassin.net;
s=dreamhost; t=1710532098;
bh=kfQfePROgyWR4EEyH9fyVwuIzNipDKI0dR3XgudkTFs=;
h=Date:Subject:To:From:Content-Type:Content-Transfer-Encoding;
b=kwKsHNjrL2SZTY41SY2jIAJGm706KYsMMmZjy0+0v48FR92vQANw+c7gkrE+lgQ+/
701LqZRFkaGHiQ0eKMIYikErwJx57oz2GpwV1jOiYKfx4p0RTyB81DZ71OMJ2MamLA
h5P295u/ztvOkKMx2dPyyfN6x4QiBzHOuNezRttl8kQGnf639iZ8DTXF0/9Ofx7W95
WvvYLt9tlobhkcxQUDdJxqRBmLl/uDaSL3CbDRtX/Zr3ZNwIwZMx7MH01tWb04aizK
/LOTa1TaGe1Cs19HwLmcjzjXa0Jbzsxex72061Bx8WM5GwrLygff118sVAyFbNqAgk
qb/C9+eHD1ZNg==
User-Agent: Mozilla Thunderbird
Content-Language: en-US
In-Reply-To: <4TxDMW3Dn1znVFX@mail.python.org>
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
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: <b169e599-e81f-45ce-8e2a-7027b59a4627@tompassin.net>
X-Mailman-Original-References: <87y1ajdeqs.fsf@zedat.fu-berlin.de>
<3cbc94a7-bf8e-4ee7-ae70-ba8b73ec61bd@tompassin.net>
<4TxDMW3Dn1znVFX@mail.python.org>
 by: Thomas Passin - Fri, 15 Mar 2024 19:48 UTC

On 3/15/2024 3:09 PM, Grant Edwards via Python-list wrote:
> On 2024-03-15, Thomas Passin via Python-list <python-list@python.org> wrote:
>> On 3/15/2024 5:30 AM, Loris Bennett via Python-list wrote:
>>> Hi,
>>>
>>> I am initialising an object via the following:
>>>
>>> def __init__(self, config):
>>>
>>> self.connection = None
>>>
>>> self.source_name = config['source_name']
>>> self.server_host = config['server_host']
>>> self.server_port = config['server_port']
>>> self.user_base = config['user_base']
>>> self.user_identifier = config['user_identifier']
>>> self.group_base = config['group_base']
>>> self.group_identifier = config['group_identifier']
>>> self.owner_base = config['owner_base']
>>>
>>> However, some entries in the configuration might be missing. What is
>>> the best way of dealing with this?
>>>
>>> I could of course simply test each element of the dictionary before
>>> trying to use. I could also just write
>>>
>>> self.config = config
>>>
>>> but then addressing the elements will add more clutter to the code.
>>>
>>> However, with a view to asking forgiveness rather than
>>> permission, is there some simple way just to assign the dictionary
>>> elements which do in fact exist to self-variables?
>>>
>>> Or should I be doing this completely differently?
>>
>> self.source_name = config.get('source_name', default_value)
>>
>> Or, if you like this kind of expression better,
>>
>> self.source_name = config.get('source_name') or default_value
>
> Won't the latter version misbehave if the value of config['source_name'] has a
> "false" boolean value (e.g. "", 0, 0.0, None, [], (), {}, ...)
>
>>>> config = {}
>>>> config['source_name'] = ""
>>>> config.get('source_name') or 'default'
> 'default'

Oh, well, picky, picky! I've always like writing using the "or" form
and have never gotten bit - especially for configuration-type values
where you really do expect a non-falsey value, it's probably low risk -
but of course, you're right. In newer code I have been putting a default
into get(). And I suppose there is always the possibility that sometime
in the future an "or" clause like that will be changed to return a
Boolean, which one would expect anyway.

Re: Configuring an object via a dictionary (Posting On Python-List Prohibited)

<ut2de5$2fd1e$5@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ldo@nz.invalid (Lawrence D'Oliveiro)
Newsgroups: comp.lang.python
Subject: Re: Configuring an object via a dictionary (Posting On Python-List
Prohibited)
Date: Fri, 15 Mar 2024 21:09:25 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 32
Message-ID: <ut2de5$2fd1e$5@dont-email.me>
References: <87y1ajdeqs.fsf@zedat.fu-berlin.de>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 15 Mar 2024 21:09:25 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="923cb71133c4c2bd336d691e5929520c";
logging-data="2602030"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+FVvCw8ga1qO+Imk4Oy2fT"
User-Agent: Pan/0.155 (Kherson; fc5a80b8)
Cancel-Lock: sha1:Rn9ejsP1S++V9NUKaBwx2CnerBU=
 by: Lawrence D'Oliv - Fri, 15 Mar 2024 21:09 UTC

On Fri, 15 Mar 2024 10:30:03 +0100, Loris Bennett wrote:

> self.source_name = config['source_name']
> self.server_host = config['server_host']
> self.server_port = config['server_port']
> self.user_base = config['user_base']
> self.user_identifier = config['user_identifier']
> self.group_base = config['group_base']
> self.group_identifier = config['group_identifier']
> self.owner_base = config['owner_base']
>
> However, some entries in the configuration might be missing.

Since all the attribute names and dictionary keys are the same, the
solution is simple:

for k in \
(
'source_name',
'server_host',
'server_port',
'user_base',
'user_identifier',
'group_base',
'group_identifier',
'owner_base',
) \
:
if k in config :
setattr(self, k, config[k])
#end if
#end for

Re: Configuring an object via a dictionary

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: 2QdxY4RzWzUUiLuE@potatochowder.com
Newsgroups: comp.lang.python
Subject: Re: Configuring an object via a dictionary
Date: Fri, 15 Mar 2024 17:33:09 -0400
Lines: 11
Message-ID: <mailman.97.1710540141.3452.python-list@python.org>
References: <87y1ajdeqs.fsf@zedat.fu-berlin.de>
<3cbc94a7-bf8e-4ee7-ae70-ba8b73ec61bd@tompassin.net>
<4TxDMW3Dn1znVFX@mail.python.org>
<b169e599-e81f-45ce-8e2a-7027b59a4627@tompassin.net>
<ZfS-lbUP1bY04LNI@anomaly>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
X-Trace: news.uni-berlin.de b6IkfbuQ55c+wB7Yg1DRug4WciycDgk3Qp5knaKZ1YXg==
Cancel-Lock: sha1:5jIg9T3MZFxbh5rmSyj08bw5gVI= sha256:45eHDXzaOKqGgiSWqkDiEQzkj9OoZI7wyk/0Hz85ZWQ=
Return-Path: <2QdxY4RzWzUUiLuE@potatochowder.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=potatochowder.com header.i=@potatochowder.com
header.b=SxcKM1im; dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.010
X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; ':-)': 0.09; 'received:78':
0.09; 'anyway.': 0.16; 'from:addr:2qdxy4rzwzuuilue': 0.16;
'from:addr:potatochowder.com': 0.16; 'received:136.243': 0.16;
'received:172.58': 0.16; 'received:78.46': 0.16;
'received:78.46.172': 0.16; 'received:78.46.172.2': 0.16;
'received:sslproxy05.your-server.de': 0.16; 'received:www458.your-
server.de': 0.16; 'received:your-server.de': 0.16; 'wrote:': 0.16;
'to:addr:python-list': 0.20; 'received:de': 0.23; 'expect': 0.28;
'python-list': 0.32; 'received:136': 0.32; 'but': 0.32; 'there':
0.33; 'header:In-Reply-To:1': 0.34; 'way': 0.38; 'want': 0.40;
'maintenance': 0.67; 'future': 0.72; 'clause': 0.84; 'sometime':
0.91
DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed;
d=potatochowder.com; s=default2305; h=In-Reply-To:Content-Type:MIME-Version:
References:Message-ID:Subject:To:From:Date:Sender:Reply-To:Cc:
Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date:
Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID;
bh=s3JGTlvDR2ROiLkn0AA04wuazkLSjT6G+qERavGFewM=; b=SxcKM1imPoIWnBKtHFols6Szg8
5JcmOEHJ9w505QEr0Otsbhq7bbX/hPVQ2QLTG1iuyhRpS81VcMWjIHiXRBVEprk2C16qyD/VYPJjm
brw/zeFwrF6pakM5rwS8z1QlJtei/E5mj3InYNEZ7xIyZZCDfIUWAfdx/JnmJG9w2Y8ARHvqFgfOc
v0aFfv/KE4gJdty2NyxVLF86k9zUJQdTGMQCdpvYZNNf1k5sySFDxtqmVrPMMWhaxCCgSPnTcszI5
2jpa9/iE/F7ozNlmjZzw2ByLLhfTxtCQ/meUqfwS8Egoq14B6E2DcL+P+BT9j9PF3YbDYibVPjHTd
8YrqotzA==;
Mail-Followup-To: python-list@python.org
Content-Disposition: inline
In-Reply-To: <b169e599-e81f-45ce-8e2a-7027b59a4627@tompassin.net>
X-Authenticated-Sender: 2QdxY4RzWzUUiLuE@potatochowder.com
X-Virus-Scanned: Clear (ClamAV 0.103.10/27215/Fri Mar 15 09:31:18 2024)
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
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: <ZfS-lbUP1bY04LNI@anomaly>
X-Mailman-Original-References: <87y1ajdeqs.fsf@zedat.fu-berlin.de>
<3cbc94a7-bf8e-4ee7-ae70-ba8b73ec61bd@tompassin.net>
<4TxDMW3Dn1znVFX@mail.python.org>
<b169e599-e81f-45ce-8e2a-7027b59a4627@tompassin.net>
 by: 2QdxY4RzWzUUiLuE@potatochowder.com - Fri, 15 Mar 2024 21:33 UTC

On 2024-03-15 at 15:48:17 -0400,
Thomas Passin via Python-list <python-list@python.org> wrote:

> [...] And I suppose there is always the possibility that sometime in
> the future an "or" clause like that will be changed to return a
> Boolean, which one would expect anyway.

Not only is the current value is way more useful, but changing it would
be a compatibility and maintenance nightmare.

If I want Java, I know where to find it. :-)

Re: Configuring an object via a dictionary

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: PythonList@DancesWithMice.info (dn)
Newsgroups: comp.lang.python
Subject: Re: Configuring an object via a dictionary
Date: Sat, 16 Mar 2024 11:23:38 +1300
Organization: DWM
Lines: 149
Message-ID: <mailman.98.1710541433.3452.python-list@python.org>
References: <87y1ajdeqs.fsf@zedat.fu-berlin.de>
<2e09744d-7f46-4c47-a439-b8a2f69d750e@DancesWithMice.info>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: news.uni-berlin.de jjSbFlQXWBKfeFktc+zq+Q9y7D3h61ajyc5shpAWA4tQ==
Cancel-Lock: sha1:BI8I4rJjrejbu7okSoRysA8hOew= sha256:9SXcbdKlFGwSll/9QLgnj++N4brRGNdsrY4hEKmnaJo=
Return-Path: <PythonList@DancesWithMice.info>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=danceswithmice.info header.i=@danceswithmice.info
header.b=PNvsukBa; dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.022
X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; '(which': 0.04; 'def':
0.04; '(for': 0.05; 'class.': 0.07; 'suggestion': 0.07; '=dn':
0.09; 'concerns,': 0.09; 'from:addr:danceswithmice.info': 0.09;
'from:addr:pythonlist': 0.09; 'like,': 0.09; 'neat': 0.09;
'question:': 0.09; 'situations': 0.09; 'aids': 0.16; 'arguments':
0.16; 'assuming': 0.16; 'attributes': 0.16; 'below).': 0.16;
'bennett': 0.16; 'command-line': 0.16; 'entirety': 0.16; 'idea!':
0.16; 'improves': 0.16; 'message-id:@DancesWithMice.info': 0.16;
'prepended': 0.16; 'pythonic': 0.16; 'question,': 0.16;
'received:51.254': 0.16; 'received:51.254.211': 0.16;
'received:51.254.211.219': 0.16; 'received:cloud': 0.16;
'received:rangi.cloud': 0.16; 'scenario,': 0.16; 'whatever.':
0.16; 'wider': 0.16; 'wrote:': 0.16; 'values': 0.17; 'code.':
0.17; 'to:addr:python-list': 0.20; 'maybe': 0.22; 'skip:_ 10':
0.22; 'code': 0.23; 'idea': 0.24; '(and': 0.25; 'again,': 0.26;
'classes': 0.26; 'object': 0.26; 'creating': 0.27; 'fact': 0.28;
'purpose': 0.28; 'environment': 0.29; 'this?': 0.29; 'header:User-
Agent:1': 0.30; 'whole': 0.30; 'default': 0.31;
'header:Organization:1': 0.31; '(as': 0.32; 'elements': 0.32;
'python-list': 0.32; 'said,': 0.32; 'structure': 0.32;
'received:192.168.1': 0.32; 'but': 0.32; "i'm": 0.33; 'there':
0.33; 'same': 0.34; 'mean': 0.34; 'skip:" 20': 0.34; 'header:In-
Reply-To:1': 0.34; 'trying': 0.35; 'meaning': 0.35; 'built': 0.36;
'work,': 0.36; 'source': 0.36; 'those': 0.36; "skip:' 10": 0.37;
'using': 0.37; 'class': 0.37; 'received:192.168': 0.37; 'way':
0.38; 'could': 0.38; 'least': 0.39; 'single': 0.39; 'this,': 0.39;
'use': 0.39; 'prompt': 0.39; '(see': 0.40; 'data.': 0.40;
'define': 0.40; 'something': 0.40; 'should': 0.40; 'view': 0.60;
'best': 0.61; 'detail': 0.61; 'method': 0.61; 'skip:o 10': 0.61;
'identified': 0.62; 'data,': 0.63; 'simply': 0.63; 'providing':
0.63; 'pass': 0.64; 'definition': 0.64; 'received:51': 0.64;
'thus': 0.64; 'similar': 0.65; 'required': 0.65; 'time.': 0.66;
'bad': 0.67; 'time,': 0.67; 'items': 0.68; 'apply.': 0.69;
'change.': 0.69; 'following:': 0.69; 'within': 0.69; 'knowing':
0.71; 'little': 0.73; 'confidence': 0.76; 'eight': 0.76;
'supposed': 0.76; 'breaking': 0.78; 'more.': 0.82; 'extra': 0.84;
'attribute': 0.84; 'brackets': 0.84; 'easy.': 0.84; 'exceptions':
0.84; 'extracted': 0.84; 'impacts': 0.84; 'method,': 0.84;
'not...': 0.84; 'thus,': 0.84; 'warning': 0.84; 'criticism': 0.91;
'variation': 0.91
DKIM-Filter: OpenDKIM Filter v2.11.0 vps.rangi.cloud 65FF34DDE
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=danceswithmice.info;
s=staff; t=1710541425;
bh=hOJIBq4dXxpA130/m4K70NA4W+9R2+MYrKnWoc7OBmE=;
h=Date:From:Subject:To:References:In-Reply-To:From;
b=PNvsukBaj9qn5L5vgLrM6nZgqUlmi7RReEt5ve0CzYucVDc2xlnnh/kwW+1Qlabnq
CNcfH0MxeErbpRGwSPShuhW7MGsjNbiYlKaV0GIldpkrd+suCDwXvOoa3GuE/Spbn4
M81/ga5Aacji+NUqIOzRO3lcpwH8cXL9CgKGdZfTvcfeDVqR5LY4ZFb4zLoEKjJa5/
0XAabyMqaoVc0JMCMVf17LyXrNwmnjz/X3sq+8CFfHcRXzKQ5Z4bFTAO07W/6pbtDJ
MI+A5zunJO7H5hwb8wH47eyN1C0+/Y88LP5iKV8tIyypHb2L2mmKU0RsuOSvon7zHJ
vjxt+Z2bVxlBA==
User-Agent: Mozilla Thunderbird
Content-Language: en-US
In-Reply-To: <87y1ajdeqs.fsf@zedat.fu-berlin.de>
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
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: <2e09744d-7f46-4c47-a439-b8a2f69d750e@DancesWithMice.info>
X-Mailman-Original-References: <87y1ajdeqs.fsf@zedat.fu-berlin.de>
 by: dn - Fri, 15 Mar 2024 22:23 UTC

On 15/03/24 22:30, Loris Bennett via Python-list wrote:
> Hi,
>
> I am initialising an object via the following:
>
> def __init__(self, config):
>
> self.connection = None
>
> self.source_name = config['source_name']
> self.server_host = config['server_host']
> self.server_port = config['server_port']
> self.user_base = config['user_base']
> self.user_identifier = config['user_identifier']
> self.group_base = config['group_base']
> self.group_identifier = config['group_identifier']
> self.owner_base = config['owner_base']
>
> However, some entries in the configuration might be missing. What is
> the best way of dealing with this?

How do you define "missing"?

Thus, @Thomas' suggestion may/not apply. It is neat and easy.

I usually plump for:

self.source_name = config[ "source_name" ] or default_value

but @Grant's warning applies!
(which is why the pythonic way is to use (and test for) None as a
definition of "missing" (see also impacts of using default values and
mutable data-structures)

LBYL cf EAFP:
When setting-up an environment like this, elements are often set to a
default-value, and then user-settings, command-line arguments, and the
like, applied in a priority-order sequence, amend as-appropriate. In
this way, such problems will never arise.

This is the better course 90% of the time.

Which raises the next question:

What is the impact if some attribute is "missing"? ie if the 'whatever'
must be identified by source_name (for example), then there is little
point in assigning any values to the new class. Considerations which
apply *after* this question, the __init__(), are at least
equally-important considerations (see below)!

> I could of course simply test each element of the dictionary before
> trying to use. I could also just write
>
> self.config = config
>
> but then addressing the elements will add more clutter to the code.

By which you mean that such "clutter" should only appear in the
__init__() - which is not such a bad idea (but see below).

OTOH it is often helpful to one's comprehension to be given a prompt as
to the source of the data. Thus, in later processing *config[
"source_name" ] may add a small amount of extra information to the
reader, over self.source_name.

* maybe prepended "self.", or not...

Am assuming that passing all eight elements as individual arguments is
off-the-table, embodying far too many 'negatives' (see below).

> However, with a view to asking forgiveness rather than
> permission, is there some simple way just to assign the dictionary
> elements which do in fact exist to self-variables?

Assuming config is a dict:

self.__dict__.update( config )

will work, but attracts similar criticism - if not "clutter" then an
unnecessary view (and understanding) of the workings of classes
under-the-hood.

Another question:
When these values are used, are they all used at the same time, and
never again? It may only be worth 'breaking-out' and making attributes
from those which are used in multiple situations within the class's
methods. If, the other extreme, they are only (effectively) passed from
the __init__() to some sort of open() method, then pass the
data-structure as an whole and delegate/remove the "clutter" to there.
In that scenario, such detail would *only* has meaning *and* purpose in
the open() method and thus no point in cluttering-up the __init__() with
detail that is only required elsewhere!

> Or should I be doing this completely differently?

YMMV, but I prefer the idea of transferring the environment/config as a
whole (as above).

If it were a class (cf the supposed dict) then "clutter" is reduced by
eschewing brackets and quotation-marks, eg "config.source_name".

If those eight-elements are not the entirety of that data-structure,
then consider creating an interface-class, which is extracted (or built
that way) from some wider 'environment', and used to set-up access to
data-source(s) or whatever. Again, this aids understanding in knowing
where data has been created/gathered, and improves confidence when
utilising it down-the-line.

The other principle in only providing the required (eight) items of
data, is that the 'receiving-class' needs no understanding of the
structure or workings of the 'sending-class' = separation of concerns,
and each class has single purpose/reason to change.

A variation on that might be to use a method/function as the interface:

access = Access( config.access_data )

Thus, the config class (instance) will need an access_data method to
collate the data-items. The complimentary code in this Access.__init__(
self, config, ) might be something like:

(
self.source_name,
self.server_host,
self.server_port,
self.user_base,
self.user_identifier,
self.group_base,
self.group_identifier,
self.owner_base = config_access()
)

If you know my style/preferences, notice that I'm breaking my own 'rule'
of using named-parameters in preference to positional-parameters when
there are three or more. However, this *may* be one of those exceptions
(cf hobgoblins). That said, this is the third and least-preferred idea!

--
Regards,
=dn

Re: Configuring an object via a dictionary

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: list1@tompassin.net (Thomas Passin)
Newsgroups: comp.lang.python
Subject: Re: Configuring an object via a dictionary
Date: Fri, 15 Mar 2024 19:08:23 -0400
Lines: 15
Message-ID: <mailman.99.1710544111.3452.python-list@python.org>
References: <87y1ajdeqs.fsf@zedat.fu-berlin.de>
<3cbc94a7-bf8e-4ee7-ae70-ba8b73ec61bd@tompassin.net>
<4TxDMW3Dn1znVFX@mail.python.org>
<b169e599-e81f-45ce-8e2a-7027b59a4627@tompassin.net>
<ZfS-lbUP1bY04LNI@anomaly>
<092b12f3-b131-44d5-af19-1e3346d0f7df@tompassin.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: news.uni-berlin.de 4nXxpuDrwm0+1rMOcqbGSA9R+MRrRIFxLRAFfXw3ZDoQ==
Cancel-Lock: sha1:1PDuEJ7ldah70pb/CKWGIAWlsgA= sha256:wYruOHFguMmvJZKfget1K/EV1PWxQDSLIZLHllXgXUA=
Return-Path: <list1@tompassin.net>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=tompassin.net header.i=@tompassin.net header.b=GpOvBHOK;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.048
X-Spam-Evidence: '*H*': 0.90; '*S*': 0.00; ':-)': 0.09; 'dan': 0.09;
'anyway.': 0.16; 'received:10.0.0': 0.16; 'received:64.90': 0.16;
'received:64.90.62': 0.16; 'received:64.90.62.162': 0.16;
'received:dreamhost.com': 0.16; 'wrote:': 0.16; 'pm,': 0.19;
'to:addr:python-list': 0.20; 'expect': 0.28; 'header:User-
Agent:1': 0.30; 'python-list': 0.32; 'received:10.0': 0.32;
'received:mailchannels.net': 0.32;
'received:relay.mailchannels.net': 0.32; 'but': 0.32; "i'm": 0.33;
'there': 0.33; 'header:In-Reply-To:1': 0.34; 'way': 0.38; 'want':
0.40; 'maintenance': 0.67; 'header:Received:6': 0.67;
'received:64': 0.67; 'future': 0.72; 'clause': 0.84; 'sometime':
0.91
X-Sender-Id: dreamhost|x-authsender|tpassin@tompassin.net
ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1710544105; a=rsa-sha256;
cv=none;
b=d0B60l48FGdP6yQqCUGWvkQ3EKVyrIBlUXfw6hSDdz/MbmYLEGKUZVoD4ZMne2JvuO7Au3
i3KTJQ2llEXNbQDel9R0L4wHTZdPPHFASaX4pBn2PpcvUvbgyjnY31irxQniMc2WDFJ71C
fH0spkQGEPTOik16+UTchOB3jvUIMSimyoKZvBbnJv7JwE35Wg7zms4/TNpKtVOqug3vb5
8P8mQeymVpvvXkWWonej+/5RSGP3xw7CpOyJFkHJY7L3dKvDnX5wqAMlK6WOO9bFnqTyP9
YKcMv741YmlWFOxttQtl8edu22LXz6kBU2ssitT8q8uX0x0APPotABryvr71Hw==
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed;
d=mailchannels.net; s=arc-2022; t=1710544105;
h=from:from:reply-to:subject:subject:date:date:message-id:message-id:
to:to:cc:mime-version:mime-version:content-type:content-type:
content-transfer-encoding:content-transfer-encoding:
in-reply-to:in-reply-to:references:references:dkim-signature;
bh=8qbouh+liJ1tiVGgjWacI7kn0F1hpypPyUzC8ZMvPeo=;
b=oKmGCfqq7QeSZq/4ejEhoJmjswS1ufwNnLclFY/xeAvsBje8LucNoLzYYZwtf67F9HEugC
ldU9PfXQTMJRMxWG4/klxCde32NKT/a29BSkzepKiPn3ek1Q+fRcxu6xxOm6t+tGbbytOD
MN0BtBQbvNYVLODnQ/COjnQ3iWQXj3MUub285DCFx9VPyo3IfsmfOdMdJa4Rm/EpdFtxoD
BZIBIpc2NPonswj7MoJtOBqTmj9QpWMs99MQXob843uNI/GQ2153nWQVXMFgvW+2s4mPF/
E+AFLDD6etdrW/muykVCe7mjR3/iLLtmMDazfGscYf+k0WOFs0wUBGo0yXQTZw==
ARC-Authentication-Results: i=1; rspamd-b46fcdc5-b6xcn;
auth=pass smtp.auth=dreamhost smtp.mailfrom=list1@tompassin.net
X-Sender-Id: dreamhost|x-authsender|tpassin@tompassin.net
X-MC-Relay: Neutral
X-MailChannels-SenderId: dreamhost|x-authsender|tpassin@tompassin.net
X-MailChannels-Auth-Id: dreamhost
X-Slimy-Thread: 2ef5cfb335159272_1710544105505_1077519266
X-MC-Loop-Signature: 1710544105505:1070308102
X-MC-Ingress-Time: 1710544105504
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tompassin.net;
s=dreamhost; t=1710544105;
bh=8qbouh+liJ1tiVGgjWacI7kn0F1hpypPyUzC8ZMvPeo=;
h=Date:Subject:To:From:Content-Type:Content-Transfer-Encoding;
b=GpOvBHOKyq2rX86pj8srbFpjbDwONUMrBVP1Kn49Av+JfN+jMownWT1LtxRy9oUjQ
TYWlKlhevOCgX94DCBmWGbzhWlmqJcNA9ehaqVtIk+KurdHr6VQaDW8jE8IU6rHf+B
sRq+eejN+GaHKxe0yWFG03uH5MNQwK7sauf83Enjiiu6m6oOPh4Ti6doR2Jw8kBISP
ObQD8ZhIugKLnfKNjDVHPOfks7BjyoYn3C5JDUqO1/PWPwd3JIl3G7J5JT3Z4ecU6N
Dw/xz+LAtA+Q+KVdKmE5OhUbRJ+MD2erWf5Gt9cp1f58kpUyELmCfqzRilvW5Pepcy
Y8TPX/VaArFEQ==
User-Agent: Mozilla Thunderbird
Content-Language: en-US
In-Reply-To: <ZfS-lbUP1bY04LNI@anomaly>
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
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: <092b12f3-b131-44d5-af19-1e3346d0f7df@tompassin.net>
X-Mailman-Original-References: <87y1ajdeqs.fsf@zedat.fu-berlin.de>
<3cbc94a7-bf8e-4ee7-ae70-ba8b73ec61bd@tompassin.net>
<4TxDMW3Dn1znVFX@mail.python.org>
<b169e599-e81f-45ce-8e2a-7027b59a4627@tompassin.net>
<ZfS-lbUP1bY04LNI@anomaly>
 by: Thomas Passin - Fri, 15 Mar 2024 23:08 UTC

On 3/15/2024 5:33 PM, Dan Sommers via Python-list wrote:
> On 2024-03-15 at 15:48:17 -0400,
> Thomas Passin via Python-list <python-list@python.org> wrote:
>
>> [...] And I suppose there is always the possibility that sometime in
>> the future an "or" clause like that will be changed to return a
>> Boolean, which one would expect anyway.
>
> Not only is the current value is way more useful, but changing it would
> be a compatibility and maintenance nightmare.

I'm with you here!

> If I want Java, I know where to find it. :-)

RE: Configuring an object via a dictionary

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: <avi.e.gross@gmail.com>
Newsgroups: comp.lang.python
Subject: RE: Configuring an object via a dictionary
Date: Fri, 15 Mar 2024 21:06:31 -0400
Lines: 45
Message-ID: <mailman.100.1710551196.3452.python-list@python.org>
References: <87y1ajdeqs.fsf@zedat.fu-berlin.de>
<3cbc94a7-bf8e-4ee7-ae70-ba8b73ec61bd@tompassin.net>
<4TxDMW3Dn1znVFX@mail.python.org>
<b169e599-e81f-45ce-8e2a-7027b59a4627@tompassin.net>
<ZfS-lbUP1bY04LNI@anomaly>
<003301da773e$2f54b850$8dfe28f0$@gmail.com>
Mime-Version: 1.0
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: 7bit
X-Trace: news.uni-berlin.de ZEGLHz9Eo75qCQXAzz2v1Q3ci668hMiip3YvuI+/ao1w==
Cancel-Lock: sha1:H8Dk1YCUVNFXh3ok6hr7U5mU/A4= sha256:hLG924ts6TFqtCTytKIUdUrl8eWMG/xX9vcdi9bFf9k=
Return-Path: <avi.e.gross@gmail.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=gmail.com header.i=@gmail.com header.b=lQoczvN4;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.109
X-Spam-Level: *
X-Spam-Evidence: '*H*': 0.78; '*S*': 0.00; 'fairly': 0.05; ':-)':
0.09; 'configuring': 0.09; 'dan': 0.09; 'expression': 0.09;
'received:108': 0.09; 'url:mailman': 0.15; '2024': 0.16;
'anyway.': 0.16; 'evaluated': 0.16; 'follows': 0.16; 'languages.':
0.16; 'wrote:': 0.16; 'python': 0.16; 'code.': 0.17; 'message-
id:@gmail.com': 0.18; 'to:addr:python-list': 0.20; 'all,': 0.20;
'language': 0.21; 'to:no real name:2**1': 0.22; 'skip:- 10': 0.25;
'url-ip:188.166.95.178/32': 0.25; 'url-ip:188.166.95/24': 0.25;
'actual': 0.25; 'url:listinfo': 0.25; 'url-ip:188.166/16': 0.25;
'anyone': 0.25; 'behavior': 0.26; 'brings': 0.26; 'friday,': 0.26;
'object': 0.26; 'function': 0.27; 'expect': 0.28; 'takes': 0.31;
'flow': 0.31; 'url-ip:188/8': 0.31; 'concept': 0.32; 'context':
0.32; 'python-list': 0.32; 'but': 0.32; 'there': 0.33; 'march':
0.33; 'same': 0.34; 'header:In-Reply-To:1': 0.34;
'received:google.com': 0.34; 'from:addr:gmail.com': 0.35;
'really': 0.37; 'received:209.85': 0.37; 'way': 0.38;
'received:209': 0.39; 'use': 0.39; 'serious': 0.40; 'wants': 0.40;
'both': 0.40; 'something': 0.40; 'want': 0.40; 'should': 0.40;
'view': 0.60; 'above': 0.62; 'from:': 0.62; 'to:': 0.62; 'seen':
0.62; 're:': 0.64; 'thus': 0.64; 'back': 0.67; 'maintenance':
0.67; 'types,': 0.69; 'rules': 0.70; 'care': 0.71; 'future': 0.72;
'easy': 0.74; 'demand': 0.75; 'sent:': 0.78; 'returned': 0.81;
'left': 0.83; 'clause': 0.84; 'control.': 0.84; 'corners': 0.84;
'rare': 0.84; 'truth': 0.86; 'sometime': 0.91; 'trick': 0.91;
'viewed': 0.93
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20230601; t=1710551194; x=1711155994; darn=python.org;
h=thread-index:content-language:content-transfer-encoding
:mime-version:message-id:date:subject:in-reply-to:references:to:from
:from:to:cc:subject:date:message-id:reply-to;
bh=W/A4PpBCba++NmUFpVGoP87UJbnA99/dDMX7m0eJ5pY=;
b=lQoczvN4xen90ZyzVk+UQDwZNbGnXD3Khze4rP2vPBoChQbrCdKA7sAor8NAfyYrzq
ttJkDAQkx0nFrtKAjxzbMXBUH5+MS0B8H+dBdlEBDdI9xJH5AFF89vtADwGEDQz0kAQU
jhKewPuizUc8vjdBuJ9/uRmzFUG3HDZqb2aUwYjGU8MkLp+DVisK0cN1An+iAsfhG+Bz
VuBUqUowszW8eXT7B/nU8j2i8B+034ErL1APlRp74KE1iH7ZnIIEwOKvNjqC0nxsfB1h
4/SdSQomZqYJh+OFvGOXu+P9ajVegSJz4atjo1WOj43tGGmYa41vsPrEYC/Mm/E38/Ao
t9dg==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20230601; t=1710551194; x=1711155994;
h=thread-index:content-language:content-transfer-encoding
:mime-version:message-id:date:subject:in-reply-to:references:to:from
:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to;
bh=W/A4PpBCba++NmUFpVGoP87UJbnA99/dDMX7m0eJ5pY=;
b=Omz6djVrlhXeWtvLOE9qSUGiCC3cRJfXzLIta4bi09LlmAv0QZEDpNspfcIoIi2oXk
PhTiOpCW/aBZnNpZVE4hLqcD++oxVOFBT080/pNc9lRhDNRL9XSUbLU77x5MDtnv21sL
BT+W5OswFVf2jV3K9fr5HhMweTImUf0CtAw24EGph1BTPX4+wnVgR5lfwiS7bCm0JxMk
bPlNk0s+yc6286HcH52KwAD4kScR4xDbkn45uaJQY1UGZsoD+z2MkCo2zFgy+GiBt234
FlGV+rwxtviklvfYFDFUZiPPVsuDCix2sd7SRWA5fcBslXaMMNQeydWGOhy1mtw8EJO9
5L6Q==
X-Forwarded-Encrypted: i=1;
AJvYcCX3rAHuFmMdCj8J23HVgn3uxti72BZlVy+vFsezqVEHaldlnddWHy6BCFNL2RE+d8saLwNOPeBce741e6B5h2Mom7FTgUuX
X-Gm-Message-State: AOJu0Yzk+swXjv4qwKeylwLVGd8yHMVPS9d4VYz0xn2xozKZYOLbkIx4
dRsWB90AwPyVZzDWXbKVHxLM3tRzkeKYbQ/LiUnQDetC5+9ZSwruBKO+X1qX
X-Google-Smtp-Source: AGHT+IECVtVceVZ4ltEsSUqcSdiGI1ELGKYP1nwFyQlXL3yAXgrq/isguwvpudCIcr9ats+LFH2GRQ==
X-Received: by 2002:a25:b2a2:0:b0:dc6:dc58:8785 with SMTP id
k34-20020a25b2a2000000b00dc6dc588785mr3844378ybj.62.1710551194140;
Fri, 15 Mar 2024 18:06:34 -0700 (PDT)
In-Reply-To: <ZfS-lbUP1bY04LNI@anomaly>
X-Mailer: Microsoft Outlook 16.0
Content-Language: en-us
Thread-Index: AQGbUsfGY+YVTHPodJtuAaQrJD0tlwFwPc2SAXG/5usDGuqK/gC9BPJisYIHXlA=
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
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: <003301da773e$2f54b850$8dfe28f0$@gmail.com>
X-Mailman-Original-References: <87y1ajdeqs.fsf@zedat.fu-berlin.de>
<3cbc94a7-bf8e-4ee7-ae70-ba8b73ec61bd@tompassin.net>
<4TxDMW3Dn1znVFX@mail.python.org>
<b169e599-e81f-45ce-8e2a-7027b59a4627@tompassin.net>
<ZfS-lbUP1bY04LNI@anomaly>
 by: <avi.e.gross@gmail.com> - Sat, 16 Mar 2024 01:06 UTC

A part of the Python view of the world is about a concept of whether
something is "truthy" or not and thus many corners of the language do not
care what kind of object an expression returns. If the object is returned in
a context looking for not a Boolean value but a truth value, it is evaluated
and in other scenarios, left alone to propagate in the code.

Changing such behavior would be a very serious undertaking, and frankly,
silly.

But if anyone really wants an actual Boolean, then the non-not operator
should do the trick as !(whatever) takes what follows as a truthy value and
negates it and a second ! brings it back as a True/False as in !!(whatever)

And for many data types, perhaps all, you can use the bool() function that I
believe follows the same rules about being truthy.

Both of the above should be fairly easy to use in any rare contexts that
demand a more standard Boolean result as in some other languages.

It is one of many strengths of python that supports varieties of
polymorphism. And it allows a value to be passed or returned that can both
be viewed as some kind of object of many kinds and seen as a Boolean for
considerations like flow of control.

-----Original Message-----
From: Python-list <python-list-bounces+avi.e.gross=gmail.com@python.org> On
Behalf Of Dan Sommers via Python-list
Sent: Friday, March 15, 2024 5:33 PM
To: python-list@python.org
Subject: Re: Configuring an object via a dictionary

On 2024-03-15 at 15:48:17 -0400,
Thomas Passin via Python-list <python-list@python.org> wrote:

> [...] And I suppose there is always the possibility that sometime in
> the future an "or" clause like that will be changed to return a
> Boolean, which one would expect anyway.

Not only is the current value is way more useful, but changing it would
be a compatibility and maintenance nightmare.

If I want Java, I know where to find it. :-)
--
https://mail.python.org/mailman/listinfo/python-list

Re: Configuring an object via a dictionary

<875xxk2g87.fsf@zedat.fu-berlin.de>

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!usenet.goja.nl.eu.org!3.eu.feeder.erje.net!feeder.erje.net!fu-berlin.de!uni-berlin.de!not-for-mail
From: loris.bennett@fu-berlin.de (Loris Bennett)
Newsgroups: comp.lang.python
Subject: Re: Configuring an object via a dictionary
Date: Mon, 18 Mar 2024 07:42:32 +0100
Organization: FUB-IT (ex-ZEDAT), Freie Universität Berlin
Lines: 22
Message-ID: <875xxk2g87.fsf@zedat.fu-berlin.de>
References: <87y1ajdeqs.fsf@zedat.fu-berlin.de> <ut25q8$2dtqs$1@dont-email.me>
<ut275n$2e6lj$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain
X-Trace: news.uni-berlin.de EXx+YaLq/kv6dvPSFtUDSAf6sBndOHdknlZ/bRfketkytR
Cancel-Lock: sha1:cT6VnDnO+SViaEn9OWPfm7nRV5Y= sha1:ycuP45iHCKH/BvGoh3c2Gc0rMX8= sha256:EVCyN18xJazPD14l7SaUHWMf6tRELMtey4/1W7ffsyM=
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux)
 by: Loris Bennett - Mon, 18 Mar 2024 06:42 UTC

Tobiah <toby@tobiah.org> writes:

> I should mention that I wanted to answer your question,
> but I wouldn't actually do this. I'd rather opt for
> your self.config = config solution. The config options
> should have their own namespace.
>
> I don't mind at all referencing foo.config['option'],
> or you could make foo.config an object by itself so
> you can do foo.config.option. You'd fill it's attributes
> in the same way I suggested for your main object.

Thanks for the thoughts. I'll go for self.config = config after
all, since, as you say, the clutter caused by the referencing is not
that significant.

Cheers,

Loris

--
This signature is currently under constuction.

Re: Configuring an object via a dictionary

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!fu-berlin.de!uni-berlin.de!not-for-mail
From: ajm@flonidan.dk (Anders Munch)
Newsgroups: comp.lang.python
Subject: Re: Configuring an object via a dictionary
Date: Mon, 18 Mar 2024 13:57:57 +0000
Lines: 38
Message-ID: <mailman.117.1710782734.3452.python-list@python.org>
References: <87y1ajdeqs.fsf@zedat.fu-berlin.de>
<2e09744d-7f46-4c47-a439-b8a2f69d750e@DancesWithMice.info>
<VI1PR05MB106808FECA8C100C3FF2F92C4B42D2@VI1PR05MB10680.eurprd05.prod.outlook.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: quoted-printable
X-Trace: news.uni-berlin.de 495k3NUnhJTigdeG8iHCcA0mhAf5fMKSHLiltrSo86KQ==
Cancel-Lock: sha1:UyDyTqdU7z/2i5QO8nsiFjYSYyU= sha256:h7WlUi8v6nq6/U+0lAIoruKh5IUgrP09UgjLemWS6M8=
Return-Path: <ajm@flonidan.dk>
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=FlonidanAS.onmicrosoft.com header.i=@FlonidanAS.onmicrosoft.com
header.b=Z2KMW76c; dkim-adsp=none (unprotected policy);
dkim-atps=neutral
X-Spam-Status: OK 0.071
X-Spam-Evidence: '*H*': 0.87; '*S*': 0.01; 'def': 0.04; 'access,':
0.09; 'int': 0.09; 'bennett': 0.16; 'collisions': 0.16; 'wrote:':
0.16; 'to:addr:python-list': 0.20; 'skip:_ 10': 0.22; 'to:name
:python-list@python.org': 0.24; 'fact': 0.28; 'elements': 0.32;
'there': 0.33; 'header:In-Reply-To:1': 0.34; 'using': 0.37;
'class': 0.37; 'way': 0.38; 'view': 0.60; 'gives': 0.62; 'skip:t
40': 0.64; 'updating': 0.64; 'risk': 0.71; '....': 0.76;
'attribute': 0.84
ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none;
b=Kg52uA9eJhf7jNtL0oNTT5Bu4YxexdAWg+VV0GDimGlAMkdcDJddjHks1DuHtR4lZrpaEZzU467UVRaKBttk7uRuBaZF6VqUB8PzU75FOCCCuprGM66Y8Ub3rEVDVi80KF45Qsg0Ro6eIWUUlMabDz046pdY8pkhqvRnKxtTofFlQEVnyxCAxRkpWGv6c2xFvTZHQPMqZMbg+WDkZxkjPw2IMZ+korRgbbNdxIOW/9X+OrGeQrD1v1bgVCfVGz/GoD7IiyULZiXcWrZT998mlwmJGtRbtZpuZGk92QlL5rmIhPI+32cjYaI4L6wMpyn8avaGEdWytgcgXPjn7wwkoA==
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com;
s=arcselector9901;
h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1;
bh=+6Seg8N7TT7iqc1lIMgi4xBPN4iRTULYU7or1v2K7Bc=;
b=lyKKRzBFsnEFiySzBBL2Qgo7WYmLKTVJrraKgtPPIgOJqNrHf8ftRZy/Z4srV0GcmdlSy7d0lWV5vabLP5ZPcmKfbLF29x33RUJzbmKJTnbA+6MgFAdHDlyg2LupCkoqhrC5ZGS0P6sbxb0BHadRPG6nJVqm7OBspkpOLwucMEMidYK6K4gipDza0nUaSP39MBquOQQr9bMP8+51jI5hJZOXn6Tsxzu3AdrMqMy61MOXPi9q/gV5fec6+KvxoGtGvPUiEvlhCZZG66Q86V+L+DgIyHyzXFTxBMaQITcypvHIOvbcrYffhE/3jds3JpPz+0B5VArHBslR3nxD9p5vQw==
ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass
smtp.mailfrom=flonidan.dk; dmarc=pass action=none header.from=flonidan.dk;
dkim=pass header.d=flonidan.dk; arc=none
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=FlonidanAS.onmicrosoft.com; s=selector2-FlonidanAS-onmicrosoft-com;
h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck;
bh=+6Seg8N7TT7iqc1lIMgi4xBPN4iRTULYU7or1v2K7Bc=;
b=Z2KMW76cBJIHGdYEVPrWHcL5nLwmWwHeSmsub8mtGATuS6b6lQlW1komftL/DyBNu88UbkyCDW77lmbhujqprJgKQIVb6PO7uRhRHFysIEH42RppJ4gPYsmwHggAEJ9x2MAhOe4iAy26vj0QP91K9aiXRTLlO5Kk8P0HKx9qJso=
Thread-Topic: Configuring an object via a dictionary
Thread-Index: AQHadwUWjPJ0bq9Fk0+AF9iOKfyasLE5YNAAgAQgCMA=
In-Reply-To: <2e09744d-7f46-4c47-a439-b8a2f69d750e@DancesWithMice.info>
Accept-Language: en-US
Content-Language: en-US
X-MS-Has-Attach:
X-MS-TNEF-Correlator:
authentication-results: dkim=none (message not signed)
header.d=none;dmarc=none action=none header.from=flonidan.dk;
x-ms-publictraffictype: Email
x-ms-traffictypediagnostic: VI1PR05MB10680:EE_|AM9PR05MB7827:EE_
x-ms-office365-filtering-correlation-id: 5b772887-7a79-4d6e-d9c8-08dc47536a9e
x-ms-exchange-senderadcheck: 1
x-ms-exchange-antispam-relay: 0
x-microsoft-antispam: BCL:0;
x-microsoft-antispam-message-info: feByR991RW2GF+/yLFF7tk0lLdm0mSJY3cY9PZ0nB3ylcxlZ1KxIGVXoPyFrLEK3kMwtA1GNPLfi9NdBEjuA3MRkCEuydqwnWipbiyRWEe8wJr2mKS9z3eNJgLSb9n8scj8mQ8UT74STWzvxuiCP6KE7mHHqkNktgjmEDqEWJKDPTiEd6c/NFu1JtgGlXJth7fiEy1okrNq/WwW5sH4uERK10oWn1CF9S17ah5tr4aQkBjqSpVo7onXBua2TgDF0YMkXhgWn0UjLKKICiph37AXXE8uyzQwaJ+YVvFAfFLQBxIrK9TMHA5rmJFCp+xJyN7zwyL6dbnFoaczzKXOR/OfHiwPkHR5DTZjck6Vk2/K73RF/2v5gUCW9iwTNBNfYfNrPZ10sa7DOG+ia3pKOvYjYLTvnB3wbePQoGYkUfzges1q7lnAFMw7h52qnriXNGiakiiuvOPFiO5REnVgARMVXm/ut7+VCcP5lx/kntDN8JWk1TX1/SHGRwWJz9d7EzOn3WU9s8lpVaCa0DUsi47PeUXSGfbDwSjaKIU329HHTjUjwJeSNktcSW9MFMxU2WGkQcGejmPoQs+0cEAThmXrVIQ5P3oCmBk8MJrkbKLMAYKb5es/xout0Ch2S3peEyuVSQsYCqCeqfLN6xo2TRUaI//bmv7TSGMWLG8w5XqomggIY4hKhxRod++l2Wc8mx9l4MoAs1Qs25dYMOBuQ9w==
x-forefront-antispam-report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:;
IPV:NLI; SFV:NSPM; H:VI1PR05MB10680.eurprd05.prod.outlook.com; PTR:; CAT:NONE;
SFS:(13230031)(1800799015)(366007)(376005)(38070700009); DIR:OUT; SFP:1102;
x-ms-exchange-antispam-messagedata-chunkcount: 1
x-ms-exchange-antispam-messagedata-0: MSx45429KBiTt2J0UbzDdcITZNeIUaRZtkc3QLGxQ/hEaWQRN5JpOfe311Pb
7giTQedUNWzsweVl7fIPcc4/0drGt+lsBEEU845+TwsgNWBBYEPVnJIfdgDw
rlHeX0ib8YRsYJpGXVFBrtwkk2BwnM7K2LvN1BQYYXfdeg3ge/dp29CZ8P4i
Tjro2bHZqlmQVvqruKWgeb2hCAJbqAWNosqC1Rx2RrhRwTk56gCbPep0wwTd
8O+ZcY/b6GKT/iA5L33VdhRrhqE1OO7fx7RsfbwGMByaZX6luqZUkeWMt5Wf
3VdIVWywCpFlqNAH2LIZJJbPj2PZKVCZPzkI9hCY+GkBRhuF5uv+FsZe5/le
j4g8YAFhEKn7U5srdgr3cK5X0A4huBkjWzG2RTF9XAELzVnxaQc0hIeQMcHF
DcPz4dxuqppfY6zNpOsknaYxmAtkP/f+WuC/4Ke5/5V22+YxIp/E7AOYb3jZ
3fRDkPz0TAepkrtrI8wovr+YcAIb6w3npA3Y19hvfbFwC5izALfuI8HHAd8l
QdkXhjv0POPV3U/IqVtbElQ+zG2oW5tFV3glp7dSTJr3VcYGmgZ5LFfpR9pb
kJ0AhJ9c2HEmybSlWoi5mJy9ipC2kt92eIXp7SD8DSaiU00NtG/BocN5q+Fi
ld2Uyr0/KGQ6CmIbj6pQZ5+4ceyOfCcn3ETV2AIYLgMRN941WWh1D80oVJ4T
cjj3WacbmqlqamdJnEfHuNkvPAnrf16cLsdkErNQ7jIeYdKji80+DcnJA9zt
XZqXuV9uoXfLml+NDbv5ErAUCzGp8dtZtkKdToGAqUc9riLmDsMAQ6BRHByJ
YGVPLp0xQMZU0TmSRr+0eCAbjUJqh5QbbaHQ1D5IyHuStDtDZPwRNtRGBWRf
0KJIhj8hAnZvWIE8gSVt8Zm8oEOjbDl2GQEaY7nys9cvzhKxwgPl+Bv46SUe
o6mGJhWdUlfRgQ5oANr+PLZ+d842N1PqWPCxJy5StgYe6WQ1o/rdlQBFERuL
Eg/HehPhDzEB7pqBUUw6QceXiODe19/xX1NEUEmpKAf/cE6tbc6HLrPzqQ0s
o138BvLjSDclEu0CHK9RMxUiOZN2Z4ymgNNZimOvitUF9OcWx+jbmlNXZdzq
t8ZtetH3dJZoFM49k4AXnXfCsQe2S59p0KAaf2YxUpEUc7auKc3jFwkPUTAx
jv3ehrBt4EheP1LhRxmATFBfmVox5qaKqDJE2CGPwCY70Jr8geJM/7g8flx4
cchHSU/JfjY633iYtS7ubwP7//G67oAWJZdmIr3nayvSY2ac8Mhy1n8OnTJW
Rf2UHcJPdf5+8aY3CveunmbjalFdA/CwAjGjYIFgzzbATClmkelAYB4rov1D
TqT6I+V12laiN//3C4hn7v+9nt88IM7UAU9NoCEp4u2mcVzV0jDTsv21gwF5
mKKPJ3906BQ+3z7vk16pFTynez6cE+WHNmUC+JKE1e6f0SSLRwBHYTIQy2ad
Su1AG5O9G9ihDB4qrP05H0rDyGrZQ+743MDcujX2qn4M8nKsGV6FascScRXv
NbpL++hAWh5Grqt7jsA=
X-OriginatorOrg: flonidan.dk
X-MS-Exchange-CrossTenant-AuthAs: Internal
X-MS-Exchange-CrossTenant-AuthSource: VI1PR05MB10680.eurprd05.prod.outlook.com
X-MS-Exchange-CrossTenant-Network-Message-Id: 5b772887-7a79-4d6e-d9c8-08dc47536a9e
X-MS-Exchange-CrossTenant-originalarrivaltime: 18 Mar 2024 13:57:57.5982 (UTC)
X-MS-Exchange-CrossTenant-fromentityheader: Hosted
X-MS-Exchange-CrossTenant-id: 66f919b0-b207-4d78-ae72-d81c18f86ec5
X-MS-Exchange-CrossTenant-mailboxtype: HOSTED
X-MS-Exchange-CrossTenant-userprincipalname: ISagKF7yh9pD6hh6jxvf6ZIMv3M+w5s47Qx/nud7PFZXXc82CI38N+2ULCiucaBX
X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM9PR05MB7827
X-Mailman-Approved-At: Mon, 18 Mar 2024 13:25:32 -0400
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
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: <VI1PR05MB106808FECA8C100C3FF2F92C4B42D2@VI1PR05MB10680.eurprd05.prod.outlook.com>
X-Mailman-Original-References: <87y1ajdeqs.fsf@zedat.fu-berlin.de>
<2e09744d-7f46-4c47-a439-b8a2f69d750e@DancesWithMice.info>
 by: Anders Munch - Mon, 18 Mar 2024 13:57 UTC

dn wrote:
>Loris Bennett wrote:
>> However, with a view to asking forgiveness rather than
>> permission, is there some simple way just to assign the dictionary
>> elements which do in fact exist to self-variables?
>
>Assuming config is a dict:
>
> self.__dict__.update( config )

Here's another approach:

config_defaults = dict(
server_host='localhost',
server_port=443,
# etc.
) ....
def __init__(self, config):
self.conf = types.SimpleNamespace(**{**config_defaults, **config})

This gives you defaults, simple attribute access, and avoids the risk of name collisions that you get when updating __dict__.

Using a dataclass may be better:

@dataclasses.dataclass
class Settings:
group_base : str
server_host : str = 'localhost'
server_port : int = 443
....
def __init__(self, config):
self.conf = Settings(**config)

regards, Anders

Re: Configuring an object via a dictionary

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: pokemonchw@gmail.com (Pokemon Chw)
Newsgroups: comp.lang.python
Subject: Re: Configuring an object via a dictionary
Date: Tue, 19 Mar 2024 10:48:02 +0800
Lines: 40
Message-ID: <mailman.119.1710899937.3452.python-list@python.org>
References: <87y1ajdeqs.fsf@zedat.fu-berlin.de> <ut25q8$2dtqs$1@dont-email.me>
<ut275n$2e6lj$1@dont-email.me> <875xxk2g87.fsf@zedat.fu-berlin.de>
<CAKZ0ktS_JKYqEanO-a=OBP3mQr2AatYAoMD-as819KahwCQCfg@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Trace: news.uni-berlin.de +Zt9BPr3MsD4IZowopcSiAkPAQ0wT/29s3jcDTMkubkA==
Cancel-Lock: sha1:cvr4Afw8C2N0jEOv4wLPKoruIsk= sha256:Es368jttPCBeXt3v9j76gbMi4L7HqLVKMMSLLvH9F1w=
Return-Path: <pokemonchw@gmail.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=gmail.com header.i=@gmail.com header.b=etQkkHjy;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.014
X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'def': 0.04; '"""': 0.09;
'cc:addr:python-list': 0.09; 'writes:': 0.09; 'cheers,': 0.11;
'&gt;': 0.14; 'cc:no real name:2**0': 0.14; 'url:mailman': 0.15;
'attributes': 0.16; 'bennett': 0.16; 'dict': 0.16; 'question,':
0.16; 'received:209.85.210.49': 0.16; 'received:mail-
ot1-f49.google.com': 0.16; 'referencing': 0.16;
'cc:addr:python.org': 0.20; 'all,': 0.20; 'skip:_ 10': 0.22;
'code': 0.23; "i'd": 0.24; 'url-ip:188.166.95.178/32': 0.25; 'url-
ip:188.166.95/24': 0.25; 'url:listinfo': 0.25; 'cc:2**0': 0.25;
'url-ip:188.166/16': 0.25; 'object': 0.26; 'solution.': 0.26;
'email addr:python.org&gt;': 0.28; 'received:209.85.210': 0.29;
'url-ip:188/8': 0.31; 'python-list': 0.32; "wouldn't": 0.32;
'message-id:@mail.gmail.com': 0.32; 'but': 0.32; "i'll": 0.33;
'same': 0.34; 'fill': 0.34; 'header:In-Reply-To:1': 0.34;
'received:google.com': 0.34; 'from:addr:gmail.com': 0.35;
'currently': 0.37; 'main': 0.37; "it's": 0.37; 'received:209.85':
0.37; 'class': 0.37; 'this.': 0.37; 'way': 0.38; 'could': 0.38;
'thanks': 0.38; 'received:209': 0.39; 'want': 0.40; 'should':
0.40; "there's": 0.61; "you'd": 0.64; 'your': 0.64; 'named': 0.65;
'skip:f 20': 0.75; 'signature': 0.76; 'email name:&lt;python-
list': 0.84; 'say,': 0.84; 'caused': 0.86; 'opt': 0.89
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20230601; t=1710816493; x=1711421293; darn=python.org;
h=cc:to:subject:message-id:date:from:in-reply-to:references
:mime-version:from:to:cc:subject:date:message-id:reply-to;
bh=4Rh5QExbSyoQdXJl5eaMSUIrpmHhfscZlVH7FLvi5b8=;
b=etQkkHjyw7R+z4zESWI0qSO7SBuEVbNKGQDrVszS0HwfWAXR8Hd7i27epI5qXuXw0T
6DF6yZUukCF3hyyZ5nEc3aoxNXryTivrQl9KXLCqV3AvSKBp6dsH0WrARNpw1HQ+M1V7
FnPrv6e9w8uMQeJG/UGJDhDN3SB44dZrjsCT6tAyf5JsezikIhQSduM0CfrmOWvScpTZ
IuZDw+Fa2iH9tKptG/uC0cOEyiHzDaz4AWWdQYAOo6OwrKNTSzmRLUJZanUW9aCSQyb1
e6D/Xvz8oFOi/qyEUGgFJgETKrpaHydxXB/HPLJD/61z8jGchd9XB25EqOb/DPqQIVZa
icEA==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20230601; t=1710816493; x=1711421293;
h=cc:to:subject:message-id:date:from:in-reply-to:references
:mime-version:x-gm-message-state:from:to:cc:subject:date:message-id
:reply-to;
bh=4Rh5QExbSyoQdXJl5eaMSUIrpmHhfscZlVH7FLvi5b8=;
b=WmtuPU76wHJvck+YugiPF4QvUHwTuvxGjQmeXMMwaTKxnXAQ8EJ4QOVUU2eCFS9mU0
8WnrasjrdGfjnSp9i/mayl7uzdwVtgkEBCE5tZ0cTztwcCDkulYWkXNzqL9pypR7jdi7
XM34dyplMJZz/fBdkzwiB4y5Whdj+dZqqoCSrkwdbMIjJxTBgmgE3PwfjCCLf9tb6eL9
MUgky09rM1xS1cWh84vtJhjHLoAvKS8y+g9HA3QAp1bxgBDX/lYH5r3zxGeqkGOuwARe
0FL39dbNIV5WC5GTN9Vv/pT7h1zxswPJYwGUDb1856jIQLoma5c5lK1ru0xZtMZDiO3S
Wfnw==
X-Gm-Message-State: AOJu0Yy9+EU+7BsknFas/A47TGHm/+1qs3Ac6+Aaxr4FyFGfMO7tjaeE
L1hdeWrGqTVW9zIL2/09KsMavTbw8WUL9sWbD/GWIxq/jxsSyeP2acyXKNTxzRMq6CTGZj2KMtv
gv7zJVpNm2iIDdCXaatIBzcxIWoM=
X-Google-Smtp-Source: AGHT+IG20jHkYThCvakwmVrZEt4oHODVlNBZzy+ftUnOoxLChQRr9P/DhPsE2qWh5tKWUEsDNmf48/Y2qiZuxTWpzZg=
X-Received: by 2002:a05:6870:3924:b0:221:c7c2:925a with SMTP id
b36-20020a056870392400b00221c7c2925amr1654826oap.14.1710816493540; Mon, 18
Mar 2024 19:48:13 -0700 (PDT)
In-Reply-To: <875xxk2g87.fsf@zedat.fu-berlin.de>
X-Mailman-Approved-At: Tue, 19 Mar 2024 21:58:55 -0400
X-Content-Filtered-By: Mailman/MimeDel 2.1.39
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
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: <CAKZ0ktS_JKYqEanO-a=OBP3mQr2AatYAoMD-as819KahwCQCfg@mail.gmail.com>
X-Mailman-Original-References: <87y1ajdeqs.fsf@zedat.fu-berlin.de>
<ut25q8$2dtqs$1@dont-email.me>
<ut275n$2e6lj$1@dont-email.me> <875xxk2g87.fsf@zedat.fu-berlin.de>
 by: Pokemon Chw - Tue, 19 Mar 2024 02:48 UTC

It's too complicated, there's no need for this
def __init__(self, config):

self.__dict__ = config
self.connection = None
"""
other code .....
"""
Note that you need to keep the fields in the config dict named the same as
the fields you want to be assigned to in your class

Loris Bennett via Python-list <python-list@python.org> 于2024年3月19日周二
01:39写道:

> Tobiah <toby@tobiah.org> writes:
>
> > I should mention that I wanted to answer your question,
> > but I wouldn't actually do this. I'd rather opt for
> > your self.config = config solution. The config options
> > should have their own namespace.
> >
> > I don't mind at all referencing foo.config['option'],
> > or you could make foo.config an object by itself so
> > you can do foo.config.option. You'd fill it's attributes
> > in the same way I suggested for your main object.
>
> Thanks for the thoughts. I'll go for self.config = config after
> all, since, as you say, the clutter caused by the referencing is not
> that significant.
>
> Cheers,
>
> Loris
>
> --
> This signature is currently under constuction.
> --
> https://mail.python.org/mailman/listinfo/python-list
>

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor