Базовый пример

In [1]:
import math


class Circle:
    def __init__(self, x, y, r):
        self.x = x
        self.y = y
        self.r = r

    def area(self):
        return math.pi * self.r ** 2


c = Circle(0, 0, 1)
print(c.x)
print(c.y)
print(c.r)
c.r = 2
print(c.r)
print(c.area())
0
0
1
2
12.566370614359172

Свой геттер и сеттер

In [2]:
import math


class Circle:
    def __init__(self, x, y, r):
        self.x = x
        self.y = y
        self.set_r(r)

    def area(self):
        return math.pi * self.r ** 2

    def set_r(self, r):
        if r < 0:
            raise ValueError("Radius should be nonnegative")
        self._r = r

    def get_r(self):
        return self._r


c = Circle(0, 0, 1)
print(c.x)
print(c.y)
c.set_r(2)
print(c.get_r())
c.set_r(-1)
0
0
2
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-2-009b147cbfbf> in <module>()
     25 c.set_r(2)
     26 print(c.get_r())
---> 27 c.set_r(-1)

<ipython-input-2-009b147cbfbf> in set_r(self, r)
     13     def set_r(self, r):
     14         if r < 0:
---> 15             raise ValueError("Radius should be nonnegative")
     16         self._r = r
     17 

ValueError: Radius should be nonnegative

Использование @property

In [3]:
import math


class Circle:
    def __init__(self, x, y, r):
        self.x = x
        self.y = y
        self.r = r

    def area(self):
        return math.pi * self.r ** 2

    @property
    def r(self):
        return self._r

    @r.setter
    def r(self, r):
        if r < 0:
            raise ValueError("Radius should be nonnegative")
        self._r = r


c = Circle(0, 0, 1)
print(c.x)
print(c.y)
c.r = 2
print(c.r)
c.r = -1
0
0
2
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-3-01991a0b2c7c> in <module>()
     27 c.r = 2
     28 print(c.r)
---> 29 c.r = -1

<ipython-input-3-01991a0b2c7c> in r(self, r)
     18     def r(self, r):
     19         if r < 0:
---> 20             raise ValueError("Radius should be nonnegative")
     21         self._r = r
     22 

ValueError: Radius should be nonnegative

Специальные __методы__

In [4]:
import math


class Circle:
    def __init__(self, x, y, r):
        self.x = x
        self.y = y
        self.r = r

    def area(self):
        return math.pi * self.r ** 2

    @property
    def r(self):
        return self._r

    @r.setter
    def r(self, r):
        if r < 0:
            raise ValueError("Radius should be nonnegative")
        self._r = r

    def __str__(self):
        return 'Circle: x={}, y={}, r={}'.format(self.x, self.y, self.r)

    def __repr__(self):
        return 'Circle(x={}, y={}, r={})'.format(self.x, self.y, self.r)

    def __lt__(self, other):
        return self.r < other.r

    def __add__(self, other):
        return Circle(self.x + other.x, self.y + other.y, self.r + other.r)


c = Circle(0, 0, 1)
print(c)
print(repr(c))
c2 = Circle(x=0, y=0, r=1)
print(c2)
print(c < c2)
print(c + c2)
Circle: x=0, y=0, r=1
Circle(x=0, y=0, r=1)
Circle: x=0, y=0, r=1
False
Circle: x=0, y=0, r=2