Re: Configuring an object via a dictionary

Liste des GroupesRevenir à cl python 
Sujet : Re: Configuring an object via a dictionary
De : list1 (at) *nospam* tompassin.net (Thomas Passin)
Groupes : comp.lang.python
Date : 15. Mar 2024, 20:48:17
Autres entêtes
Message-ID : <mailman.96.1710532109.3452.python-list@python.org>
References : 1 2 3 4
User-Agent : Mozilla Thunderbird
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.

Date Sujet#  Auteur
15 Mar 24 * Configuring an object via a dictionary15Loris Bennett
15 Mar 24 +- Re: Configuring an object via a dictionary1Mats Wichmann
15 Mar 24 +- Re: Configuring an object via a dictionary1Thomas Passin
15 Mar 24 +* Re: Configuring an object via a dictionary4Tobiah
15 Mar 24 i`* Re: Configuring an object via a dictionary3Tobiah
18 Mar 24 i `* Re: Configuring an object via a dictionary2Loris Bennett
19 Mar 24 i  `- Re: Configuring an object via a dictionary1Pokemon Chw
15 Mar 24 +- Re: Configuring an object via a dictionary1Grant Edwards
15 Mar 24 +- Re: Configuring an object via a dictionary1Thomas Passin
15 Mar 24 +- Re: Configuring an object via a dictionary (Posting On Python-List Prohibited)1Lawrence D'Oliveiro
15 Mar 24 +- Re: Configuring an object via a dictionary12QdxY4RzWzUUiLuE
16 Mar 24 +- Re: Configuring an object via a dictionary1dn
16 Mar 24 +- Re: Configuring an object via a dictionary1Thomas Passin
16 Mar 24 +- Re: Configuring an object via a dictionary1<avi.e.gross
18 Mar 24 `- Re: Configuring an object via a dictionary1Anders Munch

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal