newb question about @property

Steve D'Aprano steve+python at pearwood.info
Sat Sep 30 19:03:16 EDT 2017


On Sun, 1 Oct 2017 08:47 am, Bill wrote:

> I spent a few hours experimenting with @property. To my mind it seems
> like it would be preferable to just define (override) instance methods
> __get__(), __set__(), and possibly __del__(), as desired, as I could
> easily provide them with "ideal" customization. Am I overlooking something?

Probably.

This is a particularly simple example, with only getters. How would you write it
by overriding __get__?


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

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

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

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



-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list