[Python-de] Re: Code Style Review

Marc Haber <[email protected]>
Newsgroups gmane.comp.python.general.german
Organization private site, see http://www.zugschlus.de/ for details
Message-ID <[email protected]>
[email protected] wrote:
>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üsselwort
>> benutzt wurde ergibt das für mich keinen Sinn
>
>Auch wenn es radikal klingen mag; Es hat keinen Sinn "global" zu 
>verwenden, auch wenn Python das im Sprachumfang hat. Ein "global" hat 
>sehr viele Nebeneffekte und birgt potentielle Risiken, die den Gewinn 
>nicht aufwiegen. Es gibt andere Lösungen.

Und die wären? Jeder einzelnen Funktion in jedem einzelnen Aufruf das
config-Hash als Parameter mitgeben?

>> Gerade für die Konfiguration halte ich die globale Variable auch für
>> den vernünftigen Weg, das wirklich überall bekannt zu machen.
>
>Nein, ist es nicht. Das ist nur eine "Abkürzung". Besser wäre hier eine 
>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 
>exists!'
>                 f' Use "{self.__class__.__name__}.instance()" to access 
>it.')
>
>         # Remember the instance as the one and only singleton
>         __class__._instance = self

Das sind 22 Zeilen für was? Und da habe ich immer noch nicht das
Problem gelöst, dass ich das einzelne Objekt dieser Klasse immer noch
als globale Variable mitziehen muss oder jede einzele Funktion in
jedem einzelnen Aufruf dieses Objekt als Parameter braucht.

Oder stehe ich hier auf dem Schlauch?

Grüße
Marc
-- 
-------------------------------------- !! No courtesy copies, please !! -----
Marc Haber         |   " Questions are the         | Mailadresse im Header
Mannheim, Germany  |     Beginning of Wisdom "     | 
Nordisch by Nature | Lt. Worf, TNG "Rightful Heir" | Fon: *49 621 72739834
_______________________________________________
python-de Mailingliste -- [email protected]
Zur Abmeldung von dieser Mailingliste senden Sie eine Nachricht an [email protected]
https://mail.python.org/mailman3/lists/python-de.python.org/
Mitgliedsadresse: [email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.