Deutsch English Français Italiano |
<mailman.95.1710529784.3452.python-list@python.org> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!fu-berlin.de!uni-berlin.de!not-for-mail From: Grant Edwards <grant.b.edwards@gmail.com> 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> Bytes: 4275 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'