Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > de.comp.lang.python > #5637
| From | "Andreas B." <ab@sysing.de> |
|---|---|
| Newsgroups | de.comp.lang.python |
| Subject | Re: "property-init"-decorator |
| Date | 2020-05-27 13:18 +0200 |
| Organization | MB-NET.NET for Open-News-Network e.V. |
| Message-ID | <ralias$a77$1@gwaiyur.mb-net.net> (permalink) |
| References | <875zci8zut.fsf@orrococo> |
Am 26.05.2020 um 17:44 schrieb Оlе Ѕtrеісhеr:
> Hallo,
>
> wir möchten in einer Klasse eine Reihe von Attributen konsistent
> speichern; d.h. es gibt Beschränkungen für die verschiedenen Attribute.
> Die Idee ist, das durch Properties zu machen:
Also die Properties verwirren mich hier. Das Beispiel an einer Stelle
auch... [siehe etwas weiter unten]
Darum eine andere Frage: Gelten die Beschränkungen eines Attributs für
alle Instanzen der Klasse?
Falls Ja, die Beschränkungen als Klassenattribute einfach hinschreiben?
class Cfg(object):
def __init__(self):
self._myprop = 1.23 # hier wirklich .myprop ?
@property
def myprop(self):
return self._myprop
_maxprop = 1.24
@myprop.setter
def myprop(self, val):
if val > self._maxprop: # als Beispiel
raise ValueError()
self._myprop = val
if __name__ == '__main__':
versuch = Cfg()
print(versuch.myprop) # gibt 1.23 aus
versuch.myprop = 1
print(versuch.myprop) # gibt 1 aus
versuch.myprop = 1.25 # wirft ValueError
print(versuch.myprop)
Grüße,
Andreas
>
> Class Cfg:
> def __init__(self);
> self._myprop = 1.23
>
> [...]
>
> @property
> def myprop(self)
> return self._myprop
>
> @myprop.setter
> def myprop(self, val):
> if val > self._maxprop: # als Beispiel
> raise ValueError()
> self._myprop = val
>
>
> Das Unschöne daran ist, dass die Initialisierung der Property im
> __init__ und damit physisch weit entfernt vom Rest erfolgt. Das ist,
> wenn man viele (>>20) Attribute hat, nicht mehr übersichtlich.
>
> Wie bringt man die enger zusammen? Ein @myprop.init gibt es ja
> offensichtlich nicht und kann man auch nicht so ohne weiteres anlegen,
> oder? Oder was wäre ein sinnvolles Pattern hier?
>
> Schöne Grüße
>
> Ole
>
Back to de.comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar
"property-init"-decorator ole-usenet-spam@gmx.net (Оlе Ѕtrеісhеr) - 2020-05-26 17:44 +0200 Re: [Python-de] "property-init"-decorator Julian Gethmann <mail.python.org@gethmann.org> - 2020-05-26 18:07 +0200 Re: [Python-de] "property-init"-decorator Hartmut Goebel <h.goebel@goebel-consult.de> - 2020-05-27 10:27 +0200 Re: "property-init"-decorator "Andreas B." <ab@sysing.de> - 2020-05-27 13:18 +0200 Re: [Python-de] "property-init"-decorator Gregor Engberding <gregor@landit.de> - 2020-05-27 17:04 +0200 Re: [Python-de] "property-init"-decorator Peter Otten <__peter__@web.de> - 2020-05-27 17:13 +0200 Re: [Python-de] "property-init"-decorator Hartmut Goebel <h.goebel@goebel-consult.de> - 2020-05-31 10:13 +0200 Re: [Python-de] "property-init"-decorator Peter Otten <__peter__@web.de> - 2020-05-31 15:37 +0200
csiph-web