Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Use of @property decorator
#10
Actually, this:
def __init__(self):
    self.variable = 5
 
@property
def variable(self):
    return self._variable
 
@variable.setter
def variable(self, value):
    self._variable = value
is NOT OK. You don't need to use setter and property in this case. You don't do anything additional in the setter and/or property. In your original code you were checking the value and raising error if not accepted value.

Example for OK:
from math import pi 

class Circle:
    def __init__(self, radius):
        self.radius = radius

    @property
    def circumference(self):
        return 2 * pi * self.radius

    @property
    def diameter(self):
        return self.radius * 2

    @diameter.setter
    def diameter(self, value):
        self.radius = value / 2



circle = Circle(2)
print(f'circumference:{circle.circumference}')
print(f'diameter: {circle.diameter}')
circle.diameter = 8
print(f'radius: {circle.radius}')
print(f'circumference: {circle.circumference}')
Note - it's possible to complicate the example by allowing user to define Circle by radius OR diameter in __init__

The second one is fine in my opinion as long as reset() is really needed and used later/elsewhere in the code or as instance method.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

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 570 Nov-10-2023, 08:09 AM
Last Post: akbarza
  Curious about decorator syntax rjdegraff42 14 2,230 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,883 Feb-07-2022, 07:29 PM
Last Post: saavedra29
  ABC Module and @property decorator, Pythonic Way? muzikman 21 5,863 Aug-18-2021, 06:08 PM
Last Post: muzikman
  @property vs __set__ / __get__ and __setattr__ / __getattr__ okhajut 1 3,395 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,074 Dec-17-2019, 08:00 PM
Last Post: jasonashaw
  strange class property KaliLinux 2 2,389 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