Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Use of @property decorator
#4
The underscore differentiates the field from the methods. Without the underscore, Celsius.temperature will always call one of the methods instead of the variable and it ends up in an infinite recursion loop. The Celsius.temperature() methods would just keep calling themselves ad infinitum.

To walk through this particular example, Celsius:
  1. Celsius.__init__(5) sets self.temperature to 5 by calling the Celsius.temperature() setter.
  2. Celsius.temperature() creates the self._temperature field and sets it to 5.
  3. Celsius.to_fahrenheit() calls the Celsius.temperature() getter.
  4. Celsius.temperature() returns the value of self._temperature.
  5. Celsius.to_fahrenheit() uses the result of Celsius.temperature() to calculate the Fahrenheit value and returns it.
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 584 Nov-10-2023, 08:09 AM
Last Post: akbarza
  Curious about decorator syntax rjdegraff42 14 2,294 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,921 Feb-07-2022, 07:29 PM
Last Post: saavedra29
  ABC Module and @property decorator, Pythonic Way? muzikman 21 5,909 Aug-18-2021, 06:08 PM
Last Post: muzikman
  @property vs __set__ / __get__ and __setattr__ / __getattr__ okhajut 1 3,419 Jun-15-2021, 03:48 PM
Last Post: snippsat
  Can property getters and setters have additional arguments? pjfarley3 2 3,102 Oct-30-2020, 12:17 AM
Last Post: pjfarley3
  decorator adamfairhall 0 1,593 Aug-18-2020, 08:38 AM
Last Post: adamfairhall
  Property price calculation oli_action 4 3,233 Jul-15-2020, 04:27 PM
Last Post: sridhar
  Problem adding keys/values to dictionary where keynames = "property" and "value" jasonashaw 1 2,091 Dec-17-2019, 08:00 PM
Last Post: jasonashaw
  strange class property KaliLinux 2 2,406 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