Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Use of @property decorator
#14
Another way, keep track of the previous values, only re calculate if they have changed.
class Vessel():
    def __init__(self, class_notation, length):
        self.class_notation = class_notation
        self.length = length
        self.previous_values = (None, None, None)

    @property
    def wave_coef(self):
        if self.previous_values[:2] == (self.class_notation, self.length):
            return self.previous_values[2]
        print('calculating wave_coef')
        table = {"R0": 1,
                 "R1": 0.9,
                 "R2": 0.8,
                 "R3": 0.7,
                 "R4": 0.6,
                 "R5": 0.5,
                 "R6": 0.4}
        service_factor = table[self.class_notation]

        if (self.length <= 100):
            CW = 0.08 * self.length * service_factor
        else:
            CW = 6 + 0.02 * self.length * service_factor

        self.previous_values = (self.class_notation, self.length, CW)
        return CW


vessel = Vessel('R2', 2)
print(vessel.wave_coef)
print(vessel.wave_coef)
vessel.class_notation = 'R4'
print(vessel.wave_coef)
vessel.length = 1
print(vessel.wave_coef)
Output:
calculating wave_coef 0.128 0.128 calculating wave_coef 0.096 calculating wave_coef 0.048
Reply


Messages In This Thread
Use of @property decorator - by ruy - Jun-08-2020, 05:11 PM
RE: Use of @property decorator - by Yoriz - Jun-08-2020, 05:31 PM
RE: Use of @property decorator - by buran - Jun-08-2020, 05:37 PM
RE: Use of @property decorator - by stullis - Jun-08-2020, 05:37 PM
RE: Use of @property decorator - by ruy - Jun-08-2020, 06:01 PM
RE: Use of @property decorator - by buran - Jun-08-2020, 06:09 PM
RE: Use of @property decorator - by ruy - Jun-08-2020, 06:19 PM
RE: Use of @property decorator - by Yoriz - Jun-08-2020, 06:27 PM
RE: Use of @property decorator - by deanhystad - Jun-08-2020, 10:57 PM
RE: Use of @property decorator - by buran - Jun-09-2020, 03:55 AM
RE: Use of @property decorator - by ruy - Jun-09-2020, 04:02 PM
RE: Use of @property decorator - by buran - Jun-09-2020, 04:27 PM
RE: Use of @property decorator - by Yoriz - Jun-09-2020, 04:34 PM
RE: Use of @property decorator - by Yoriz - Jun-09-2020, 04:48 PM
RE: Use of @property decorator - by ruy - Jun-09-2020, 04:57 PM
RE: Use of @property decorator - by Yoriz - Jun-09-2020, 05:00 PM
RE: Use of @property decorator - by buran - Jun-09-2020, 05:29 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  the order of running code in a decorator function akbarza 2 571 Nov-10-2023, 08:09 AM
Last Post: akbarza
  Curious about decorator syntax rjdegraff42 14 2,238 May-03-2023, 01:21 PM
Last Post: rjdegraff42
  Subclass initialized property used in parent class method. Is it bad coding practice? saavedra29 5 1,886 Feb-07-2022, 07:29 PM
Last Post: saavedra29
  ABC Module and @property decorator, Pythonic Way? muzikman 21 5,867 Aug-18-2021, 06:08 PM
Last Post: muzikman
  @property vs __set__ / __get__ and __setattr__ / __getattr__ okhajut 1 3,396 Jun-15-2021, 03:48 PM
Last Post: snippsat
  Can property getters and setters have additional arguments? pjfarley3 2 3,078 Oct-30-2020, 12:17 AM
Last Post: pjfarley3
  decorator adamfairhall 0 1,583 Aug-18-2020, 08:38 AM
Last Post: adamfairhall
  Property price calculation oli_action 4 3,207 Jul-15-2020, 04:27 PM
Last Post: sridhar
  Problem adding keys/values to dictionary where keynames = "property" and "value" jasonashaw 1 2,078 Dec-17-2019, 08:00 PM
Last Post: jasonashaw
  strange class property KaliLinux 2 2,390 Nov-25-2019, 04:32 PM
Last Post: KaliLinux

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020