Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: c.buhtz@posteo.jp Newsgroups: de.comp.lang.python Subject: =?utf-8?q?=5BPython-de=5D_Re=3A_Code_Style_Review?= Date: Mon, 28 Nov 2022 22:38:02 +0000 Lines: 47 Message-ID: <7255f3485a4745df69dc13cedf68e8b0@posteo.de> References: <480b80d21e58d280c437b4854bfc7cf1@posteo.de> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: quoted-printable X-Trace: news.uni-berlin.de gd3iofZ+I4E3YrHoVjKXTgg66VOzkRkUGjkxCZT56+mw== Authentication-Results: mail.python.org; dkim=pass reason="2048-bit key; unprotected key" header.d=posteo.jp header.i=@posteo.jp header.b=I/dewbSI; dkim-adsp=pass; dkim-atps=neutral DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.jp; s=2017; t=1669675082; bh=LiwBtAPBBQQ4gZrv3XuK5zd6nTHaLH5X3rjrgTCXV8c=; h=Date:From:To:Subject:From; b=I/dewbSI3lInHnDxixxl7VT4+u3MKwnebVDEUFgW+JShcsNg76JipiiGDQsmnSBvl 5V1PZ2jii6KayvwhWlsuesbNjXlFQe90jPYd8+GFJ5J5DWuL72ighhu+i5julQ6Juh 3WxZ8MwEVVC1JSwIoeTLkJ6sbMSYI+4Z2JlKuyRcxNAEfKmaWsN03+rIkVYz+BWqHv VBSs4AOMwTaiPgSrQgvFF6GweihII5oVwrvdFMUH1Z/DdvtU+gt9gdf5ZQ28z6HALG /pMiVxmjxEMVwP8yRDWl/8z3Vkfu145AWB9WE2WDU+Sr6MsNz8qRIn5eMsZ8pggHAi Wq6qtqJmiophw== Mail-Followup-To: python-de@python.org In-Reply-To: Message-ID-Hash: NPC3AW7OJ2ZJCGRCFP7QON4MQWJNUE3B X-Message-ID-Hash: NPC3AW7OJ2ZJCGRCFP7QON4MQWJNUE3B X-MailFrom: c.buhtz@posteo.jp X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-python-de.python.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.7 Precedence: list List-Id: Die Deutsche Python Mailingliste Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Xref: csiph.com de.comp.lang.python:5869 Am 28.11.2022 21:52 schrieb Marc Haber: > Das sehe ich mindestens teilweise anders. Wenn ich irgendwo explizit > hinschreibe, dass eine Funktion eine globale Variable verwendet (wie > z.B. das dictionary, in dem die Konfiguration drin steht) und der > Linter das unkonditional anmeckert nur allein weil das Schl=C3=BCsselwort > benutzt wurde ergibt das f=C3=BCr mich keinen Sinn Auch wenn es radikal klingen mag; Es hat keinen Sinn "global" zu=20 verwenden, auch wenn Python das im Sprachumfang hat. Ein "global" hat=20 sehr viele Nebeneffekte und birgt potentielle Risiken, die den Gewinn=20 nicht aufwiegen. Es gibt andere L=C3=B6sungen. > Gerade f=C3=BCr die Konfiguration halte ich die globale Variable auch f= =C3=BCr > den vern=C3=BCnftigen Weg, das wirklich =C3=BCberall bekannt zu machen. Nein, ist es nicht. Das ist nur eine "Abk=C3=BCrzung". Besser w=C3=A4re hie= r eine=20 Art Singleton-Pattern anzuwenden. Sagen wir deine Konfiguration ist in der Klasse "Config". class Config: @classmethod def instance(cls): # Provide the instance if it exists if cls._instance: return cls._instance # But don't created implicit when needed. raise Exception(f'No instance of class "{cls}" exists. ' 'Create an instance first.') def __init__(self): # Exception when an instance exists if __class__._instance: raise Exception( f'Instance of class "{self.__class__.__name__}" still=20 exists!' f' Use "{self.__class__.__name__}.instance()" to access=20 it.') # Remember the instance as the one and only singleton __class__._instance =3D self Quelle:=20 https://github.com/bit-team/backintime/blob/buhtz_config-singleton/common/c= onfig.py