Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > de.comp.lang.python > #5641

Re: [Python-de] "property-init"-decorator

From Peter Otten <__peter__@web.de>
Newsgroups de.comp.lang.python
Subject Re: [Python-de] "property-init"-decorator
Date 2020-05-31 15:37 +0200
Organization None
Message-ID <mailman.45.1590932265.24731.python-de@python.org> (permalink)
References <875zci8zut.fsf@orrococo> <4719790.vIyLdRSyTD@palindrom> <9d687032-db10-2482-9455-540de7c9a3a4@goebel-consult.de> <2456739.UVeabOu3e6@palindrom>

Show all headers | View raw


Am So Mai 31 2020, 10:13:43 schrieb Hartmut Goebel:
> Am 27.05.20 um 17:13 schrieb Peter Otten:
> > Die Reihenfolge kann durch die Positionierung in der Klasse bestimmt werden:
> Könnte man machen, allerdings würde ich mich nicht darauf verlassen,
> dass die Reihenfolge in `vars(A).keys()` immer stimmt. Kannst Du mit
> eine Doku dazu zeigen, ich habe nichts gefunden.

Was Knackiges kann ich auch nicht vorweisen, nur

https://docs.python.org/3/whatsnew/3.6.html#pep-520-preserving-class-attribute-definition-order

mit dem Verweis auf die

PEP 520 – Preserving Class Attribute Definition Order
https://www.python.org/dev/peps/pep-0520

bei deren Enstehung wohl noch nicht bekannt war, dass `dict`s die
insertion order erhalten.

> Allerdings hat mich Deine Mail noch auf einen ganz anderen Ansatz gebracht:
> 
> class A:
>     _myprop = 1.23
> 
>     @property
>     def myprop(self):
>         return self._myprop
> 
>     @myprop.setter
>     def myprop(self, value):
>         if value < 2.5:
>             self._myprop = value
> 
> a = A()
> print(a.myprop)
> a.myprop = 2.1
> print(a.myprop)

Das hat den Vorteil, dass man sich nur auf Standard-Konstrukte verlässt.
Für mutable defaults muss man das allerdings modifizieren.

Back to de.comp.lang.python | Previous | NextPrevious in thread | Find similar


Thread

"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