Deutsch English Français Italiano |
<mailman.117.1710782734.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: Anders Munch <ajm@flonidan.dk> 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: =?us-ascii?Q?MSx45429KBiTt2J0UbzDdcITZNeIUaRZtkc3QLGxQ/hEaWQRN5JpOfe311Pb?= =?us-ascii?Q?7giTQedUNWzsweVl7fIPcc4/0drGt+lsBEEU845+TwsgNWBBYEPVnJIfdgDw?= =?us-ascii?Q?rlHeX0ib8YRsYJpGXVFBrtwkk2BwnM7K2LvN1BQYYXfdeg3ge/dp29CZ8P4i?= =?us-ascii?Q?Tjro2bHZqlmQVvqruKWgeb2hCAJbqAWNosqC1Rx2RrhRwTk56gCbPep0wwTd?= =?us-ascii?Q?8O+ZcY/b6GKT/iA5L33VdhRrhqE1OO7fx7RsfbwGMByaZX6luqZUkeWMt5Wf?= =?us-ascii?Q?3VdIVWywCpFlqNAH2LIZJJbPj2PZKVCZPzkI9hCY+GkBRhuF5uv+FsZe5/le?= =?us-ascii?Q?j4g8YAFhEKn7U5srdgr3cK5X0A4huBkjWzG2RTF9XAELzVnxaQc0hIeQMcH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wP7//G67oAWJZdmIr3nayvSY2ac8Mhy1n8OnTJW?= =?us-ascii?Q?Rf2UHcJPdf5+8aY3CveunmbjalFdA/CwAjGjYIFgzzbATClmkelAYB4rov1D?= =?us-ascii?Q?TqT6I+V12laiN//3C4hn7v+9nt88IM7UAU9NoCEp4u2mcVzV0jDTsv21gwF5?= =?us-ascii?Q?mKKPJ3906BQ+3z7vk16pFTynez6cE+WHNmUC+JKE1e6f0SSLRwBHYTIQy2ad?= =?us-ascii?Q?Su1AG5O9G9ihDB4qrP05H0rDyGrZQ+743MDcujX2qn4M8nKsGV6FascScRXv?= =?us-ascii?Q?NbpL++hAWh5Grqt7jsA=3D?= 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> Bytes: 9780 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 =3D dict( server_host=3D'localhost', server_port=3D443, # etc. ) .... def __init__(self, config): self.conf =3D types.SimpleNamespace(**{**config_defaults, **con= fig}) This gives you defaults, simple attribute access, and avoids the risk of na= me collisions that you get when updating __dict__. Using a dataclass may be better: @dataclasses.dataclass class Settings: group_base : str server_host : str =3D 'localhost' server_port : int =3D 443 .... def __init__(self, config): self.conf =3D Settings(**config) regards, Anders