# property() 函式

``class Ball:    def __init__(self, radius):        self.radius = radius``

ball = Ball(1.23)
print(ball.radius)
ball.radius = 2.31

``````class Ball:    def __init__(self, radius):        if radius <= 0:            raise ValueError('必須是正數')        self.__radius = radius        def getRadius(self):        return self.__radius
def setRadius(self, radius):        if radius <= 0:            raise ValueError('必須是正數')        self.__radius = radius``````

ball = Ball(1.23)
print(ball.getRadius())
ball.setRadius(2.31)

``class Ball:    def __init__(self, radius):        if radius <= 0:            raise ValueError('必須是正數')        self.__radius = radius        def getRadius(self):        return self.__radius            def setRadius(self, radius):        self.__radius = radius            def delRadius(self):        del self.__radius            radius = property(getRadius, setRadius, delRadius, 'radius 特性說明')``

ball = Ball(1.23)
print(ball.radius)
ball.radius = 2.31

property()函式也可以使用修飾器語法，讓程式更為直覺。例如上例修改為以下：
``class Ball:    def __init__(self, radius):        if radius <= 0:            raise ValueError('必須是正數')        self.__radius = radius        @property    def radius(self):        return self.__radius            @radius.setter    def radius(self, radius):        self.__radius = radius        @radius.deleter    def radius(self):        del self.__radius``

``import mathclass Ball:    def __init__(self, radius):        if radius <= 0:            raise ValueError('必須是正數')        self.__radius = radius        @property    def radius(self):        return self.__radius        @property    def volumn(self):        return 4.0 / 3.0 * math.pi * self.__radius ** 3        @volumn.setter    def volumn(self, volumn):        if volumn <= 0:            raise ValueError('必須是正數')        self.__radius = ((3.0 * volumn) / (4.0 * math.pi)) ** (1.0 / 3.0)        ball = Ball(10.0)print(ball.volumn)ball.volumn = 5000print(ball.radius)``