Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Use of @property decorator
#1
Hi there,

I've noticed that when using the @property and @attribute_name.setter decorators all the examples I've found on the net always use an underscore before the variable name, even if in the __init__ they haven't. I tried to not use the underscore before the variable name and it doesn't work. Just wondring why?

Example code (temperature used in __init__ and _temperature in decorated methods)
# Using @property decorator
class Celsius:
    def __init__(self, temperature=0):
        self.temperature = temperature

    def to_fahrenheit(self):
        return (self.temperature * 1.8) + 32

    @property
    def temperature(self):
        print("Getting value...")
        return self._temperature

    @temperature.setter
    def temperature(self, value):
        print("Setting value...")
        if value < -273.15:
            raise ValueError("Temperature below -273 is not possible")
        self._temperature = value
Thanks a lot
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 558 Nov-10-2023, 08:09 AM
Last Post: akbarza
  Curious about decorator syntax rjdegraff42 14 2,186 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,849 Feb-07-2022, 07:29 PM
Last Post: saavedra29
  ABC Module and @property decorator, Pythonic Way? muzikman 21 5,822 Aug-18-2021, 06:08 PM
Last Post: muzikman
  @property vs __set__ / __get__ and __setattr__ / __getattr__ okhajut 1 3,374 Jun-15-2021, 03:48 PM
Last Post: snippsat
  Can property getters and setters have additional arguments? pjfarley3 2 3,065 Oct-30-2020, 12:17 AM
Last Post: pjfarley3
  decorator adamfairhall 0 1,577 Aug-18-2020, 08:38 AM
Last Post: adamfairhall
  Property price calculation oli_action 4 3,188 Jul-15-2020, 04:27 PM
Last Post: sridhar
  Problem adding keys/values to dictionary where keynames = "property" and "value" jasonashaw 1 2,069 Dec-17-2019, 08:00 PM
Last Post: jasonashaw
  strange class property KaliLinux 2 2,375 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