[Tutor] Class access rules
Dennis Lee Bieber
wlfraed at ix.netcom.com
Mon Mar 7 11:23:26 EST 2022
On Mon, 7 Mar 2022 14:54:58 +1100, Phil <phillor9 at gmail.com> declaimed the
following:
>
>Thanks Cameron, I read a brief explanation about the use the @property
>decorator but couldn't see where I could use it. I'll continue with my
>current line of class usage.
To give you one viewpoint. I've so seldom used properties that I
haven't learned the decorator view... My last use was via the property()
function! (which puts everything on one spot, vs having to know that
@property only defines a getter, and one has to then use decorators with
the name of the getter to create setter/deleter)
https://docs.python.org/3/library/functions.html#property
My favorite example is for, say, a Point() class which supports
coordinates in both Cartesian (x,y) and Polar (rho, theta).
The class would only really store one format, say x, y. Accessing rho,
theta would then be done via properties to (for read) compute the rho/theta
from x/y, and (on write) computing x/y from provided rho/theta (since only
one is set at a time, this does get a bit complex -- changing rho requires
the setter to first compute existing rho/theta from x/y, then change the
rho value, and finally recompute x/y from the new rho and calculated theta.
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed at ix.netcom.com http://wlfraed.microdiversity.freeddns.org/
More information about the Tutor
mailing list