Вы находитесь на странице: 1из 66

=======================================

# # �������� ���
# ������������:
# ������������ - ����������� ����� ������� �� ������ ����� ���������
# ���� ����� ��������� �����, ���������� ����������� ������
# ������������ ��� ������� ������, �.�. ��������� ����������� ����������
# �������� ����� ����� � �������������� ������������ � ����������� �����
# �� ���� ����������� � ��������� ������� ������ ��� ����� ������
# ����������� (�� ����. modern � �����������, ���������, ����������)
# � ��� ������� ��������� �������, ���������� ��� � ������������ � ������
�����������.
#
# �����������:
# ����������� - ����� ������� �������� ������ �� ��������
# � �����, �����������������, ������ ����� �������� ������� �������������� ������
# ��� ������ ��� ����������� � �����, ��� ����� ����� ������ �����������, ��������,
# � ��� ������� ����� ����� ���� �� �����
#
# ����������:
# ���������� - ��� ����������� ��������� ������ � ������ ������ �� ��������
�������������
# �� ���� �����, ������ �� ������ ��� ������������� ����� ��������� �����
# ������ - ������������ ����������
# �� ������ � ������� �������� ������ ����� ���������� ������
# � ���� ����� ���������� ������
#
# # �����
# ��� ����� �����?
# ����� - ��� �����-�����.
# ��� ���� �� �������� ������������ ��������� ����� ������.
# �����-���� ���� ��� �����
# ����������� �� ����� �������-����� ���� ��� ���������(�������) ����� ������

# ��������� ������:
# �� ������ Point ������ � ������� �����
# � ������ ���� ������ ��������������� (����� - ��� ����� ������, ���� ��������)
# � �������� ��� ���� (� ����� ������ - ��� ����� �� ���������, ����� ����������
������������)

# �������� ������:
# """����� �� ������������ ����� ���������"""
# �������� ��������:
# print(Point.__doc__)
# �������� ��:
# print(Point.__name__)
# �������� ��� ��������� ����������:
# print(dir(Point))

class Point:
"""����� �� ������������ ����� ���������"""
x = 1
y = 7

# print(Point.__doc__)
# print(Point.__name__)
# print(dir(Point))

pt = Point() # ������� �������� ������


# ������ ����� ��������� ���������� ���� � ��������� ������
print(pt.__dict__) # {} ������ ������, �.�. ��������� ���������� ���
# �������� ������ pt - ������������ ���, � ������� ���� ���������� x y.
# ������� ������ ��������������� �� ������ Point
# �������� �� ��������(��������) ������ Point
# ��-�������� ���������� ��������(��������) ������ Point
# ���� ����� ��� (�������� ������ ����� �� ���� ��� ����������)
print(pt.x, pt.y) # �������� ���������� x y (1 7)
Point.x = 100 # ������� �������� ���������� x
print(pt.x, pt.y) # �������� ���������� x y (100 7)
# ��� ������������� ����, ��� ���������� ������ �� ������ Point.
# �� ��� ������ �������
print(id(pt), id(Point), sep='\n')
# id 3940280
# id 4036488
# ������
# ������� ������� ���������� � ��������� ������ pt
pt.x = 24
pt.y = 37
# � ������������ ��� pt ����� ������� ���� ��������� ���������� x y
# ����� �� �������� � ������� Point.
print(pt.x, pt.y) # �������� ���������� x y (24 37)
print(Point.x, Point.y) # �������� ���������� x y (100 7)
# ������ ����� ��������� ���������� ���� � ��������� ������
print(pt.__dict__) # {'x': 24, 'y': 37} �������� ��������� ����������
# ��������� � ������ Point � � ��������� ������ pt ��������� ������� (��������)
# � ���������� (����������) ����� �������� ��� ������ �������:
# getattr(obj, name[, default]) = ���������� �������� �������� �������
# setattr(obj, name, value) = ����� �������� �������� (���� �������� ���, �� ��
�������)
# delattr(obj, name) = ������ ������� � ������ name
# hasattr(obj, name) = �������� �� ������� �������� name � obj
print(getattr(pt, 'x')) # 24
# ���� �������� 'x' ��� � ������� pt = ������
# �� �������� ����� �������� ����� ������� ��������� ��������
print(getattr(pt, 'z', False)) # False
# ������ ������� 'z' � ������ pt
setattr(pt, 'z', 44)
# ������� ������� ���� ��������� ������� pt
print(pt.__dict__) # {'x': 24, 'y': 37, 'z': 44}
# ������� ������� 'z' �� ������� pt
delattr(pt, 'z')
# ������� ������� ���� ��������� ������� pt
print(pt.__dict__) # {'x': 24, 'y': 37}
# ��� ��� ������ ������� � � ������� Point
# ��� ����� �� ������ � ������� ������ � ����������
print()
Point.z = 100 # ������ ������� 'z' � ������ Point
# ������� ������� ���� ��������� ������ Point
print(Point.__dict__)
del Point.z # ������ ������� 'z' �� ������ Point
# ������� ������� ���� ��������� ������ Point
print(Point.__dict__)
# ������ ������� 'z' � ����� Point
setattr(Point, 'z', 150)
# ������� ������� ���� ��������� ������ Point
print(Point.__dict__)
# ������� ������� 'z' �� ������ Point
delattr(Point, 'z')
# ������� ������� ���� ��������� ������ Point
print(Point.__dict__)
# �� ���� �������� ����� ������� �������� ���������
# ������ �� pt ���������� ������ Point
print(isinstance(pt, Point)) # True (pt ��� �������� ������ Point)
# ��� ������������ ����� type(obj)
print(type(pt)) # <class '__main__.Point'> pt ������ �������� ������ Point
# �������� ���
print(type(pt) == Point) # True (pt ��� �������� ������ Point)
# ������� �������� x y �� ������������ ��� ������� pt
del pt.x
del pt.y
# ������� ������� ���� ��������� ������� pt
print(pt.__dict__) # {} ����� ��������� ��������� ���
# ������ ������ pt ����� �������� �� �������� ������ Point
print(pt.x, pt.y) # 100 7
print(Point.x, Point.y) # 100 7

# ������� �� ����������� ������� �� ������ Point


def myf(obj, attr):
if hasattr(obj, attr):
delattr(obj, attr)
print(f"����� ������� {attr} �� ������� {obj}")
else:
print(f"�������� {attr} � ������� {obj} ���")
#

myf(Point, 'z') # �������� z � ������� <class '__main__.Point'> ���


myf(pt, 'z') # �������� z � ������� <__main__.Point object at 0x00831FB8> ���
pt.x = 1234
# ������� ������� ���� ��������� ������� pt
print(pt.__dict__)
myf(pt, 'x')
# ������� ������� ���� ��������� ������� pt
print(pt.__dict__)

=======================================
# # ������ ������
#
#
# class<�� ������ � ������� �����>:
# ������(���, ����������, ��������, ��������) - ��������������� (�����������
������)
# ������(�������) - ������� (�����������
�������)
# class Point:
# x = 0
# y = 0
# def setCoord(self):
# pass
# �������� self ��������� ������������� ��������������� ������
# �������� ���������� Python ���� ����� ���������� � ������
# ������ ����� ���� �������� self ������ (��� ������������)
# �������� self ������ �������� �� �������� ������, ������� ������ ���� �����
# pt = Point() ������� �������� ������ Point
# pt.setCoord() ����� ������ ���������� pt � self �������� �� ���� �������� pt
# pt2 = Point() ������� �������� ������ Point
# pt2.setCoord() ����� ������ ���������� pt2 � self �������� ��� �� �������� pt2

# class Point:
# x = 0
# y = 0
#
# def set_coord(self):
# print(self.__dict__) # ����� �� ����� ���� ��������� ����������

# pt = Point()
# pt.set_coord() # {} ��������� ���������� ���
# �������� ��������� ����������
# pt.x = 5
# pt.y = 10
# pt.set_coord() # {'x': 5, 'y': 10}
# ��������� ���������� �������� � ������ self �������� �� �������� pt
# � ��� self �������� �� �������� ��������� ����� set_coord()
# �� � ������ ����� ������������� ���������� �����
#
# class Point:
# x = 0
# y = 0
#
# def set_coord(self, x, y):
# self.x = x
# self.y = y
#
#
# pt = Point()
# pt.set_coord(10, 20)
# print(pt.__dict__) # {'x': 10, 'y': 20}
# ��������� ��������� ���������� - ��� ���������� ������ ��������� pt
# print(Point.__dict__) # 'x': 0, 'y': 0
# ����� ����� �� ���� ��������� ���������� ������ Point
# (�� ���������� � ������ ���������, � ����� �� ������ ������ Point)
# ���� �������� ������ Point ����� ������������ ��� �� �������������� �����
����������
# ����� ������ set_coord() ����� ����� Point = ������
# Point.set_coord(10, 20) = ������
# pt2 = Point()
# Point.set_coord(pt2, 30, 40) # pt2.set_coord(30, 40) ��� ������ ������������ =
���� ���������
# print(pt2.__dict__) # {'x': 30, 'y': 40}
#
#
#
#
#
# � ����� Python ��������� ���������� ������:
# ��� ������������ - �������� ������ - ��� ������������
#
#
#
# ����� __init__(self) �������������(�����������)
# �� ���������� ��������� ��� ������� ��������� ������ pt
# � ����������� �� ��� ��������� ��������� ������ pt
# ��������� ������������� ����� ������� ��������� ������ pt
# ��� ������ �������� ������ pt ������ �����������
# ����� __init__(self) ��������� ������
#
#
#
# ����� __del__(self) �����������(����������)
# ����� ��������������� ������������
# ��������� ������������� � ����� ���������� ��������� ������ pt
# ����������� ��������� ������ pt ���������� � ��� ������,
# ����� �� ���� ��������� �� �������� �� ��������� ������ pt
# ����������� ����������� ��������� ������ �������������
# pt = Point() pt �������� �� ������ Point()
# pt = 0 pt �������� �� 0 ������ Point() ����� �����
# ��� ������ �������� ������ pt ������ ���� ���������
# ����� __del__(self) ��������� ���������
#
#
# ����������� ������ �������:
class Point:
def __init__(self, x=0, y=0):
print("�������� ��������� ������ Point : " + str(self)) # self.__str__()
��� ����������
self.x = x
self.y = y

def __del__(self):
print("�������� ��������� : " + self.__str__()) # self.__str__() ���
����������

pt = Point() # �������� ��������� ������ Point : <__main__.Point object at


0x00531FB8>
print(pt) # <__main__.Point object at 0x00531FB
print(pt.__dict__) # {'x': 0, 'y': 0}
#
pt1 = Point(5) # �������� ��������� ������ Point : <__main__.Point object at
0x0054B0E8>
print(pt1) # <__main__.Point object at 0x0054B0E8>
print(pt1.__dict__) # {'x': 5, 'y': 0}
#
pt2 = Point(5, 7) # �������� ��������� ������ Point : <__main__.Point object at
0x0054B058>
print(pt2) # <__main__.Point object at 0x0054B058>
print(pt2.__dict__) # {'x': 5, 'y': 7}
# ������ ����� ����������� ���������� �������������� ��������
# �������� ��������� : <__main__.Point object at 0x00531FB8>
# �������� ��������� : <__main__.Point object at 0x0054B0E8>
# �������� ��������� : <__main__.Point object at 0x0054B058>
#
#
#
#
# ����� __new__() ��������� ����� ��������� ������� ������
# (������ ����� ���-�� ������� �� ������� ������� ������)

# class Point:
# def __new__(cls, *args, **kwargs):
# print("����� __new__ �� " + str(cls))
# return super().__new__(cls)
#
# def __init__(self, x=0, y=0):
# print("����� __init__ �� " + str(self))
# self.x = x
# self.y = y
# � ������ __new__ ����������� ������ �������� cls
# �������� cls �������� �� ������� ������ Point
# ����� ������� �������� ������, ������ ��������� ����� __new__ ������,
# �������� ��� ����� � �������� ������� ���������,
# �� ������� ������ ���� (����������� � �������) ���������.
# ��� ��������� ����� �������� � __init__
# ����� __new__ ���������� ����� ������ ���������� ���������.
# �� ����� �������� ������� ��������� return � ������ __new__
# ����� ��������� ����� __init__ ����� ��������� �� ���������� ��� �������������.
#
# � ������ __init__ ����������� ������ �������� self
# �������� self �������� �� ����������� �������� ������ Point
# �������� pt = Point() ��� self �������� �� ��������� pt
#
# cls �������� �� ��� �����, � self �������� �� ��������� ��������� ������
#
# ����� __new__ ���������� ����� ������ ���������� ��������� � ������: return
super().__new__(cls)
# super() - ��� ������ �� ������� �����
# �� �������� ������ �������� ����� �� ���������� ����� __new__ (����� ������ �����
�������� ������)
# � � ���� ������� ���������� cls - ������ �� ��� ����� Point
# � ���������� ����� __new__ �������� ������ object ��������� ������� �������
��������� ������ Point
# � ���������� ����� ������ ���������� �������
# ��� ������ � ����� Python ������������� � ����� ��������� �� �������� ������
object
#
# ������ ������� ��������� ������ � �����������
# ������� �������� ��������� � ����� __new__
# � ������ ������ (1, 2) �������� ��� *args

# pt = Point(1, 2)
# print(pt)
# ����� __new__ �� <class '__main__.Point'>
# ����� __init__ �� <__main__.Point object at 0x0071B088>
# <__main__.Point object at 0x0071B088> - ������ �� �������� ������ pt
#
#
# ������� Singleton
#
# ��������� � ������, ����� ����������
# ������� ������ ���� �������� ������.
# ��� ��������� ������� ������� ������� ������
# ��������� �� ��� ���������. (������ �������� ������ ���������� �� ������)
# �� ���� �������� ������ � ����� ������ ���� ������ ����.
# Singleton - ��� ������, ������� ������������ �������� ������ ����� ���������� /
��������.
# ������:

class DataBase:
__instance = None
def __new__(cls, *args, **kwargs):
if cls.__instance is None:
cls.__instance = super().__new__(cls)
return cls.__instance

def __del__(self):
DataBase.__instance = None

def __init__(self, user, psw, port):


self.user = user
self.psw = psw
self.port = port

def connect(self):
print(f"���������� � �� : {self.user}, {self.psw}, {self.port}")

def close(self):
print("�������� ��������� � ��")

def read(self):
return "������ �� ��"

def write(self, data):


print(f"������ � �� : {data}")

# ����������� ����� ��������� ������ ���� �������� ������� ������


# �� ����� ��������� ������: __instance = None
# ������ �� �������� ������:
# �� ������ �������� ������ - �������� None
# ������ �������� ������ - ������ �� ���� ��������
# � ���� �������� ������ ������, �� ������ ��� �� �������
# ��� �������� ���������� � ������ __new__
# ���� �������� cls.__instance ��� None
# ������� �������� ������: cls.__instance = super().__new__(cls)
# � ������� ����� ���������� ������� return cls.__instance
# ���� �������� cls.__instance ��� �� None,
# �� �� ��������� �������� ������,
# � ������� ����� ����� ���������� ������� return cls.__instance
#
# ����� ������� � ����� ����� ������ ���� �������� ������ DataBase
#
# � ������ __del__ ����� ��������� ��������� ������
# �������� __instance ������ DataBase ����� ��������� �������� None
# � ����� ����� �������� ������� �������� ������ DataBase

db = DataBase('root', 1234, 80)


db2 = DataBase('root2', 5678, 40)
print(id(db), id(db2))
# ���� id ��������� = ������� �� ���� ������: 11776304 11776304
# ��� �������� ������� ����� �������� ��� �� �������
#

db.connect() # ���������� � �� : root2, 5678, 40


db2.connect() # ���������� � �� : root2, 5678, 40
# �� ���� ���� ������ ������ db = DataBase('root', 1234, 80)
# �� � ������� �� ������ ������ �� root2, 5678, 40
# ��� ���������� ������ ���, ������ ��� ��� ������� �������
# ����� ������ ��� ������� ��������������
# � �� ����� � ����� ��������� ��������
# ����� ��� ��������� ���������� ��������������
# �� ���� ���������� ����� __call__
# def __call__(self, *args, **kwargs):
#
#
#
#
# ������ ������ (classmethod) � ����������� ������ (staticmethod) | ��� Python
#

class Vector:
MIN_COORD = 0
MAX_COORD = 100

@classmethod
def validate(cls, arg):
return cls.MIN_COORD <= arg <= cls.MAX_COORD

def __init__(self, x, y):


self.x = self.y = 0
if self.validate(x) and self.validate(y):
self.x = x
self.y = y

def get_coord(self):
return self.x, self.y

v = Vector(1, 2) # � __init__ ��������� x, y ������ ��� �������� ������� ������


�������
res = v.get_coord() # ���������� ���������� �������
print(res) # (1, 2)
# ����� get_coord() - ������� �����, ��������� ����� �������� ������: v.get_coord()
# ������� ������ ����� �������� � ��������������� ����� ����� (� ����� ������
Vector)
res = Vector.get_coord(v)
print(res) # (1, 2)
# ��� �������� �������� ������ �� ��������
# �� �������� ������ ����� (� ����� ������ v) ��� ������
# ��� �� ��� ������� ������� ������� � ������
# ���� �� ��� ���� �������: @classmethod � @staticmethod
#
# ������ ������ ���������� � ������ ���������� @classmethod :
# � ������ ��������:
# �������� ������ MIN_COORD = 0 � MAX_COORD = 0
# ����� ������ @classmethod def validate(cls):
# ������������� ������������ cls - ������ �� ����� Vector
# ����� ����������� ��������� (� ��� arg)
# �������� ��������� � �������� cls.MIN_COORD <= arg <= cls.MAX_COORD
# � ���������� True ���� False
# ������ ������ @classmethod ������� ������������ � ���������� ������,
# �� �� ����� �������� � ���������� ���������� ���������� ����� ������
# ���� ������ ������ �� ��� ����� cls � ��� ������ �� �������� ������ self
# ����� ������ ������ @classmethod ����� �������� ��������������� ����� ����� (�
����� ������ Vector)
print(Vector.validate(5)) # True

class Vector:
MIN_COORD = 0
MAX_COORD = 100

@classmethod
def validate(cls, arg):
return cls.MIN_COORD <= arg <= cls.MAX_COORD

def __init__(self, x, y):


self.x = self.y = 0
if self.validate(x) and self.validate(y):
self.x = x
self.y = y
print(Vector.norm2(self.x, self.y))

def get_coord(self):
return self.x, self.y

@staticmethod
def norm2(x, y):
return x*x + y*y

v = Vector(10, 20) # � __init__ ��������� x, y ������ ��� �������� ������� ������


�������
res = v.get_coord()
print(res)
# ���������� ���� ����� �� �������� � __init__
# if Vector.validate(x) and Vector.validate(y):
# ������ ���� ������� False x = 0 y = 0
#
# ��������� � ������������ ���������
# if self.validate(x) and self.validate(y):
# self - ��� ������ �� �������� ������
# �� ��� �� �������� ��������� � ������
# ������������� ����� Python ��������� �����
# �� ������ ������ ��������� ����� validate()
# � ��������� ��������� ������ cls - ������ �� ����� (� ��� Vector)
# ������ �������� ������ Vector, ������ ������ ����� �� ���� - ������������
#
#
#
# ������ ����������� ���������� � ������ ���������� @staticmethod :
# ������ �� ���� ������� �� � ��������� ������, �� � ��������� ���������� �����
������
# ����������� � �������������� ����� ���������� ������ ������
# ������� � ��������� ������ ������
# �������� ����������� �����, ������� �������� ����������� ����� ������-�������
(������� ����� ������� �������)
res = Vector.norm2(5, 6)
print(res) # 61
# ���� ����������� ����� ����� �������� � ������ ������ ������� ������
# print(Vector.norm2(self.x, self.y))
# �� ����� ����� self (����� �������� ������ ������ ��������� ��� ��������)
# print(self.norm2(self.x, self.y))
# � ��������� ������ � ����� ������� ����� �� ���������: return x*x + y*y +
Vector.MAX_COORD
# �������� �����, �� ��� ����� ����� ������ (� ��� Vector) ����� ������ �������� �
� ������
# ���� ����� ��������� � ��������� ������ - ��������� ������ ������ � ����������
@classmethod
#
# �����:
# ������� ������ (��� �����������) �������� �� ���������� ������
# � ����� self (������ �� ��������) ������� � ���������� ����� ��������� (� ��� x y
��������� v)
# � � ���������� ������ ������ (� ��� MIN_COORD MAX_COORD ������ Vector).
#
# ������ � ����������� @classmethod ������� ������ � ���������� ������
# � ����� cls (������ �� �����) ������� � ���������� ������ (� ��� MIN_COORD
MAX_COORD ������ Vector)
# ������ � ���������� ��������� �������� (��� self - ������ �� ��������).
#
# ������ � ����������� @staticmethod ������� ������ � ����������� ������������
��������������� � ���
# �������� ����������, ������ ����-�� ��������� ������
# � ��������� ������ � ��������� ��������� ������� �� ����
#
#
# ����������� �������� � ������ �������, ��������� @staticmethod
#
# ����������� �������� - �������� ������,
# ������� ������� � ����� ������
# � �������� �� ���� ���������� ������.
# �� ����������� �������� = �������� ��������� ������,
# ������� ������� � ����� ���������
# � �� �������� �� ���� ���������� ������.
#
#
class Point2D:
__count = 0

def __init__(self, x=0, y=0):


Point2D.__count += 1
self.__x = x
self.__y = y

def __del__(self):
print("�������� ��������� " + str(self))

# def get_count(self):
# return self.__count
@staticmethod
def get_count():
return Point2D.__count

def get_coord(self):
return self.__x, self.__y

def set_coord(self, x, y):


self.__x = x
self.__y = y

# p = Point2D()
# p1 = Point2D()
# p2 = Point2D()
# print(p.count)
# print(p1.count)
# print(p2.count)
# Point2D.count = 100
# print(p.count, p1.count, p2.count) # 100 100 100
# �������� count - ��� ����������� ��������
# ��������� �� ���� ���������� ������
# (�� �������� � ������ ������)
# �������� � ���������� ������ p1 p2
# ����������� �������� count
# p1.count = 10
# p2.count = 50
# print(p.count, p1.count, p2.count) # 100 10 50
# ������ � p1 p2 ������� ��� ��������� �������� count,
# ������� ����� �� ������� � �� ������ Point2D,
# � �������� p ��-�������� ���� �������� count �� ������ Point2D.
# ����� �� ��������� �������� __x __y
# ������� � ������ ��������� (� ������ ��������� ����)
#
# ����� �� ������� � ������� ���������� � ������
# (���� ����� � ������ ���� ��� ��� �����������)
# � ������ Point2D �������� __x __y ��� ��������� ��������
# � ��������� � �������� ��� ��������� ������� ��������
# �������� ������ ����� ������ get_coord() � set_coord()
# ������� ��������� � �������� count: __count
# � ��� ������ ������ ������ __init__(self, x=0, y=0)
# ����� ����������� �������� __count,
# ������������ ��������� ���������
# � ��������� � �������� __count
# �������� ������ ����� ������ get_count()
#
# �������� - ��� �������� � ������ ������ ��� ���������
# ����� �� ����� ��������� ������� ���������
p = Point2D()
p1 = Point2D()
p2 = Point2D()
# �������� �������� �������� __count ����� �����
# print(Point2D.get_count(p)) # 3
# ����� ������ ������ ���� �������� ������ ��� self
# �������� �������� �������� __count ����� ���� ��������
print(p.get_count()) # 3
# ������� ��� ��������� ������ Point2D
# ����� ������ ����������� �� ������� ���������
# ����� �����Point2D (����� �� ��������� � �������� ��� � �������� __count)
# ��� ��� ����� �������� ������ �� ����������� ��������� __count
# � �� ��������� � ��������� ���������� ������
# ����� ������� ������� ���� �����������
# � ������ ���������� @staticmethod
# � ������ self ���������� ��������� Point2D.__count
# � ������ ������ ����� ������ ����� ��������
# ������ �� ���������� � �������� ������ Point2D
# ������ �� ���������� � �������� ���������� ����������
print(Point2D.get_count()) # 3
print(p.get_count()) # 3
#
# �������� �������� ������� __x __y ������� ���������
print(p.__dict__) # {'_Point2D__x': 0, '_Point2D__y': 0}
print(p1.__dict__) # {'_Point2D__x': 0, '_Point2D__y': 0}
print(p2.__dict__) # {'_Point2D__x': 0, '_Point2D__y': 0}
# �������� � �������� x � ������� ������
# print(p.__x) # AttributeError: 'Point2D' object has no attribute '__x'
# print(p.x) # AttributeError: 'Point2D' object has no attribute 'x'
#
# �������� � �������� x y ����� ������ get_coord() � set_coord()
print(p.get_coord()) # (0, 0)
print(p1.get_coord()) # (0, 0)
print(p2.get_coord()) # (0, 0)
# ��������� ������� x y �� ���������� p1 p2
p1.set_coord(1, 2)
p2.set_coord(3, 5)
# �������� �������� ������� __x __y ������� ���������
print(p.get_coord()) # (0, 0)
print(p1.get_coord()) # (1, 2)
print(p2.get_coord()) # (3, 5)
#
#
# ������ ������� ������ Singleton
#
#
# �.�. ����� �� �������� ����� �������
# ������ ���� ��������
# ������� �� ������� ������ Point2D.
# ���������� ��� ����� Point2D:
#
# ������� ������� �������� __instance = None
# �������� ���������� ������ __new__
# ������� ��������� ����� ���, ��� ������� �������� ������ Point2D
# �� ����� ���� � ���� ������ � ������� ��������
# def __new__(cls, *args, **kwargs):
# if not isinstance(cls.__instance, cls):
# cls.__instance = super(Point2D, cls).__new__(cls)
# else:
# print("�������� ������ ��� ������" + str(cls.__instance))
#
# �������� cls ������ �� ����� Point2D
# ������ ������ ��������:
# if not isinstance(cls.__instance, cls)
# ���� ������� �������� cls.__instance
# �� ������� ������� ��� � ������ cls (��-�������� __instance = None)
# �� ������ �������� ������ cls.__instance = super(Point2D, cls).__new__(cls)
# ��������� � �������� ������ super
# ������� � ���, ��� ����� ���������(Point2D, cls)
# ������ ����� � �������� .__new__(cls) �������� ������
# � ������ ����������� �������� �������� cls.__instance =
# �.�. �������� None ������ �� ����� ������� ��������� ������ Point2D
# � ����� ����� � ������� ��������: "�������� ������ ��� ������"
#
class Point2D:
__count = 0
__instance = None

def __new__(cls, *args, **kwargs):


if not isinstance(cls.__instance, cls):
cls.__instance = super(Point2D, cls).__new__(cls)
else:
print("�������� ������ ��� ������"+ str(cls.__instance))

def __init__(self, x=0, y=0):


Point2D.__count += 1
self.__x = x
self.__y = y

def __del__(self):
print("�������� ��������� " + str(self))

# def get_count(self):
# return self.__count
@staticmethod
def get_count():
return Point2D.__count

def get_coord(self):
return self.__x, self.__y

def set_coord(self, x, y):


self.__x = x
self.__y = y

p = Point2D()
p1 = Point2D()
p2 = Point2D()
print(id(p), id(p1), id(p2))
#
# ����� �� �����
# �������� ������ ��� ������<__main__.Point2D object at 0x007FB0A0>
# �������� ������ ��� ������<__main__.Point2D object at 0x007FB0A0>
# 1622887656 1622887656 1622887656
# �������� ��������� <__main__.Point2D object at 0x007FB0A0>
# ������ ������ ���� �������� p = Point2D()
# ��������� ������ p1 � p2 ������� �� ��������� �������� p
# ������ � id � ���� ���� � ��� ��
#
#
=======================================
# # ������ ������� : public, private, protected / ������� � ������� / �������-
�������� (property) / �����������
#
#
# # ������������ :
# ������ ������� - public, private, protected
# ������� � ������� 1
# �������-�������� (property) � ����������� �������
#
#
# class Point:
# def __init__(self, x=0, y=0):
# self.x = x
# self.y = y
#
#
# pt = Point(1, 2)
# # �������� �� ��������
# # ����� ��������� � ��������� ������
# print(pt.x, pt.y)
# # ��� ����� �� ��������
# pt.x = 10
# pt.y = "abc"
# print(pt.x, pt.y)
#
# �� ���������� ����������� (������ � ������ ������ ����� ���������� ��� � ������)
# ��������� ������ �������:
#
# attribute (��� ������ ��� ���� ������������� �������) � ��������� ��������
(public);
#
# _attribute (� ����� ��������������) � ����� ������� protected
# (������ �� �������� ������ ������ � �� ���� ��� �������� �������)
#
# __attribute (� ���� ��������������) � ����� ������� private (������ �� ��������
������ ������ ������)
#
# ������� �������� � ���� �������������� = private
# class Point:
# def __init__(self, x=0, y=0):
# self.__x = x
# self.__y = y
#
# def set_coord(self, x, y):
# if (isinstance(x, int) or isinstance(x, float)) and \
# (isinstance(y, int) or isinstance(y, float)):
# self.__x = x
# self.__y = y
# else: print("���������� ������ ���� �������")
#
# def get_coord(self):
# return self.__x, self.__y
#
#
# pt = Point(1, 2)
# �������� �������� = private
# �� ����� ��������� � ��������� ������
# print(pt.x, pt.y) ��������� ������
# �� ������� � ��������� � ����������� ��������� ������
# ������� = ��������� ������� ��������
# ������� = ��������� ������ ��������
# ������ ��������� � ��������� ����� ������ ����� ���
# print(pt.get_coord()) # ����� ��������
# pt.set_coord(10, 20) # ��������� ��������
# print(pt.get_coord()) # ����� ��������
# ��� ���������� ����� �������� ������ �������� �������� ������
# ��� ������� � ��������: ��������� ��� ������
# if (isinstance(x, int) or isinstance(x, float)) and \
# (isinstance(y, int) or isinstance(y, float)):
# � ���� ������ ���� ���� \ ����������� �����
# ���������� ���������� �������� � ��������� �����
# pt.set_coord('10', 'abc') # ��������� ������������ ��������
# print(pt.get_coord()) # ����� �������� / ������� �� ����������
#
# ������ ����� �������� ������ � ��������� private-�����
# def __checkValue(self): ��� �� ����������� self
# �.�. ����� �� ��������� � ��������� ��������� ��������� ���������� ������
(��������)
# � ��������� � ������ ������ �������� ������ ����� �� ������
# if Point.__check_value(x) and Point.__check_value(y):
# ���� � ����� ������ �������� True, �� ���������� ����� ������� ��������� x y
# class Point:
# def __init__(self, x=0, y=0):
# self.__x = x
# self.__y = y
#
# def __check_value(a):
# if isinstance(a, int) or isinstance(a, float):
# return True
# return False
#
# def set_coord(self, x, y):
# if Point.__check_value(x) and Point.__check_value(y):
# self.__x = x
# self.__y = y
# else: print("���������� ������ ���� �������")
#
# def get_coord(self):
# return self.__x, self.__y
#
#
# pt = Point(1, 2)
# print(pt.get_coord())
# pt.set_coord('10', 'abc') # ��������� ������������ ��������
# print(pt.get_coord()) # ����� �������� / ������� �� ����������
#
# �� ����� ����, � Python ����� ������������ ����� ��������� � � ���������
��������� �����.
# ���� ����������� ��� �������� ���������:
# print(dir(pt))
# �� ����� ������ �� ������, ��������: '_Point__check_value', '_Point__x',
'_Point__y',
# _�� ������__�� ����������
# _�� ������__�� ������
# ��� � ���� ������� ����� ��������� ���������, � ������� �� ����� ��������� �����
������ pt � ����� ��:
# print(f" pt._Point__x : {pt._Point__x}")
# print(f" pt._Point__y : {pt._Point__y}")
# print(Point._Point__check_value(25))
# ������, ��� ������ ������ �� ������������ � ������� ������������� ������
��������������� ������������,
# ��� �������� � ������ ���������� ����� ������ ����� ����������� ������������
������.
# �����, �������� �������������� ������.
#
# ��� ������������� �� ����� ����������� �������������� �������� �� ����������
���������
# ���� ���������� ���������� ������� __setattr__, __getattribute__, __getattr__ �
__delattr__
#
# __setattr__(self, key, value)__ � ������������� ��������� ��� ��������� ��������
key ������;
#
# __getattribute__(self, item) � ������������� ��������� ��� ��������� ��������
������ � ������ item;
#
# __getattr__(self, item) � ������������� ��������� ��� ��������� ��������������
�������� item ������;
#
# __delattr__(self, item) � ������������� ��������� ��� �������� �������� item (��
�����: ���������� ��� ��� ���).
#
# class Point:
# MAX_COORD = 100
# MIN_COORD = 0
#
# def __init__(self, x=0, y=0):
# self.x = x
# self.y = y
#
# def set_coord(self, x, y):
# if self.MIN_COORD <= x and y <= self.MAX_COORD:
# self.x = x
# self.y = y
# else: print("�� ���������� ���� ���������")
#
# def set_bound(self, left):
# self.MIN_COORD = left
#
#
# pt1 = Point(1, 2)
# print(pt1.__dict__)
# pt2 = Point(10, 20)
# print(pt2.__dict__)
#
# ����� class Point ����� ������������ ��� ����� ������������ ���
# � ������� �������� �������� � ������
# � ����� ������:
# �������� MAX_COORD MIN_COORD
# ������ def __init__() def set_coord()
# �� ��� � ���� �������� ������ (� ����� ������ ������ ��������)
# ����� �� ������ ��������� ������� (�������)
# �� ��� ��� �������� ������ � ������ � �� �������� � ��������� ���������
# �.�. �������� ������ ������ ������ �� ���� ����������
# �� ���������� ����� ��������� � ���
# �.�. ���� �����-���� ������� ����������� � ���������,
# ���������� ��������� � ��������� ������
# print(pt1.MAX_COORD) # � pt1 ��� ������ �������� � ��������� ���������� �
��������� ������
# print(pt2.MIN_COORD) # � pt2 ��� ������ �������� � ��������� ���������� �
��������� ������
#
# ����� ��������� � ��������� ������ ������ ������-���� ������
# ����� ������� ���� ������������ ��� ������ ������
# if Point.MIN_COORD <= x <= Point.MAX_COORD:
# ���� ������������ ��� ��������� ������
# if self.MIN_COORD <= x <= self.MAX_COORD:
# pt1.set_coord(-10, 200) # �� ���������� ���� ���������
#
# ����������� ��� ����� ����� �� �������� �������� ������ MIN_COORD
# �������� public-�����
# def set_bound(self, left):
# self.MIN_COORD = left
# �� � ������ self.MIN_COORD = left �� ���������� �������� �������� ������
MIN_COORD
# ���������� ��������:
# ���� � ��������� �� ������� �������� self �� ������ ��������, �� �� �������
# ���� �� ����, �� ������ �������� �� ��������
# �.�. ������ ���������� � ��������� ��������� ������ (�������)
# print(pt1.__dict__) # {'x': 1, 'y': 2} �������� MIN_COORD � ��������� ���
# pt1.set_bound(50)
# print(pt1.__dict__) # {'x': 1, 'y': 2, 'MIN_COORD': 50} ������� ����
# print(Point.__dict__) # 'MAX_COORD': 100, 'MIN_COORD': 0, ������� �� ��������
# ������� �� �������� �������� ������ ���� ����� ����� ��������
# � ����� �������� �������� � ����� ������:
# class Point:
# MAX_COORD = 100
# MIN_COORD = 0
#
# def __init__(self, x=0, y=0):
# self.x = x
# self.y = y
#
# def set_coord(self, x, y):
# if self.MIN_COORD <= x and y <= self.MAX_COORD:
# self.x = x
# self.y = y
# else: print("�� ���������� ���� ���������")
#
# @classmethod
# def set_bound(cls, left):
# cls.MIN_COORD = left
#
#
# pt = Point(1, 2)
# print(pt.__dict__) # {'x': 1, 'y': 2}
# print(Point.__dict__) # 'MAX_COORD': 100, 'MIN_COORD': 0,
# pt.set_bound(50) # ��� ����� ��������� ����� �������� pt
# print(pt.__dict__) # {'x': 1, 'y': 2}
# print(Point.__dict__) # 'MAX_COORD': 100, 'MIN_COORD': 50,
#
# ������ �������� �������� ��������� � ����� ������
# � ��������� ������ �������� ���
#
# ���������� � ������ ���������� ������ �� ������ � ���������� ������
# class Point:
# MAX_COORD = 100
# MIN_COORD = 0
#
#
#
# def __init__(self, x=0, y=0):
# self.x = x
# self.y = y
#
# def set_coord(self, x, y):
# if self.MIN_COORD <= x and y <= self.MAX_COORD:
# self.x = x
# self.y = y
# else: print("�� ���������� ���� ���������")
#
# def __getattribute__(self, item):
# print("�������� ����� __getattribute__()")
# if item == 'x':
# raise ValueError(f"������ � �������� {item} �������")
# else:
# return object.__getattribute__(self, item)
#
# def __setattr__(self, key, value):
# if key == 'e':
# raise AttributeError("������������ �� ��������")
# else:
# print(f"�������� ����� __setattr__() : �������� {key} ���������
�������� {value}")
# object.__setattr__(self, key, value)
# # ��� ����� �������� � ��� = ��������� ����� ��,
# # �� ����� ������������ ������ ������� - ������
# # self.__dict__[key] = value
# # self.x = value # ��� ����� ����� ��������� ���������� ���� ��
�������� ���� = ������
#
# def __getattr__(self, item):
# print(f"�������� ����� __getattr__() : �������� {item} �� ���������� ")
# return False
#
# def __delattr__(self, item):
# print(f"�������� ����� __delattr__() : �������� �������� {item} ")
# object.__delattr__(self, item)
#
# ���� ����� def __getattribute__(self, item):
# ������������� ���������, ����� ��� ���������� �������� ����� �������� ������
# ����� ����� ��������� ��������� �� ������ ��������� � ������ object
# �� �������� ����� ��������� ��� ������
# ������� ����� ����� �� ����� ������ object.__getattribute__( )
# � �������� ��� ����� ����� �� ��������� object.__getattribute__(self, item)
# ����� ������� �� �������������� ���������� ����� __getattribute__
#
# def __getattribute__(self, item):
# print("�������� ����� __getattribute__()")
# return object.__getattribute__(self, item)

# pt = Point(1, 2)
# a = pt.x # �������� ����� __getattribute__()
# print(a) # 1
# a = pt.y # �������� ����� __getattribute__()
# print(a) # 2
# ������ ��� ������ ��� ��������� ������-���� �������� x ����� �������� ������ pt
# �� ����������� ���� ���������� �����
# ���� ����� ����� ������������ �� ��������� �������� � ���������
# ������� ������� �����
# def __getattribute__(self, item):
# print("�������� ����� __getattribute__()")
# if item == 'x':
# raise ValueError(f"������ � �������� {item} �������")
# else:
# return object.__getattribute__(self, item)
# ������ ��� ��������� � �������� x ����� ������ ValueError
#
# ���� ����� def __setattr__(self, key, value):
# ������������� ���������, ����� ��� ���������� ������-���� �������� �������
# def __setattr__(self, key, value):
# print(f"�������� ����� __setattr__() : �������� {key} ��������� ��������
{value}")
# object.__setattr__(self, key, value)
# pt = Point(1, 2)
# print(pt.__dict__) # {'x': 1, 'y': 2}
# pt.z = 5
# print(pt.__dict__) # {'x': 1, 'y': 2, 'z': 5}
# �������� ����� __setattr__() :
# �������� x ��������� �������� 1
# �������� ����� __setattr__() :
# �������� y ��������� �������� 2
# �������� ����� __getattribute__()
# {'x': 1, 'y': 2}
# �������� ����� __setattr__() :
# �������� z ��������� �������� 5
# �������� ����� __getattribute__()
# {'x': 1, 'y': 2, 'z': 5}

# # ���� ����� ����� ������������ �� ��������� ������� �������� � �����������


������
# pt.e = 5 # ������ AttributeError: ������������ �� ��������
#
# ���� ����� def __getattr__(self, item):
# ������������� ���������, ����� ��� ��������� � �������������� �������� ���������
������
# def __getattr__(self, item):
# print(f"�������� ����� __getattr__() : �������� {item} �� ���������� ")
# print(pt.MIN_COORD) # 0
# print(pt.z) # 5
# print(pt.ww) # ������� ������� � ������� �������� �������������� ��������
# �������� ����� __getattr__() : �������� ww �� ���������� � ����������� ������
# None # ����� ���������� ������ ��������
# ���� ����� ����� ������������ �� ���������� False ������ ������
# �� ����� ��������� ������������ �������� return False
# def __getattr__(self, item):
# print(f"�������� ����� __getattr__() : �������� {item} �� ���������� ")
# return False
# ���� ���
# def __getattr__(self, item):
# return print(f"�������� ����� __getattr__() : �������� {item} �� ����������
")
# � ������ ��� �� �� ��������
#
#
# ���� ����� def __delattr__(self, item):
# ������������� ���������, ����� ��� �������� ������-���� ��������
# del pt.z
# print(pt.__dict__)
#
# �� ��������� ���������� ��������� ��������� __slots__ = ["x", "y", "z"]
# ��� �������� �� ���������� ������ ������
# ��������� �������� ������ ������ MAX_COORD �� �� �����
#
#
#
# class Point:
# MAX_COORD = 100
# MIN_COORD = 0
#
# __slots__ = ["__x", "__y", "__z"]
#
# def __init__(self, x=0, y=0, z=0):
# self.__x = x
# self.__y = y
# self.__z = z
#
# def __check_value(x):
# if isinstance(x, int) or isinstance(x, float):
# return True
# return False
#
# def set_coord(self, x, y, z):
# if Point.__check_value(x) and Point.__check_value(y) and
Point.__check_value(z):
# self.__x = x
# self.__y = y
# self.__z = z
# else: print("���������� ������ ���� �������")
#
# def get_coord(self):
# return self.__x, self.__y, self.__z
#
#
# pt = Point(1, 2, 3)
# print(pt.get_coord()) # (1, 2, 3)
# pt1 = Point()
# print(pt1.get_coord()) # (0, 0, 0)
# pt1.e = 25 # AttributeError: 'Point' object has no attribute 'e'
# ��� ��� "e" �� �������� � ���������� ��������� __slots__ = ["__x", "__y",
"__z"]
#
#
# �������-�������� (property) � ����������� �������
#
# ��������� � ��������� ����������� �� �������� �� ������ ������
# �������� �������� ���� ����� ������� �������� �������-�������� (property)
#
# class Point:
# def __init__(self, x=0, y=0):
# self.__x = x
# self.__y = y
#
# def __check_value(a):
# if isinstance(a, int) or isinstance(a, float):
# return True
# return False
#
# def __get_x(self):
# print("����� get_x")
# return self.__x
#
# def __set_x(self, x):
# print("����� set_x")
# if Point.__check_value(x):
# self.__x = x
# else:
# raise ValueError("�������� ������ ������: x")
#
# def __get_y(self):
# print("����� get_y")
# return self.__y
#
# def __set_y(self, y):
# print("����� set_y")
# if Point.__check_value(y):
# self.__y = y
# else:
# raise ValueError("�������� ������ ������: y")
#
# def __del_coord_x(self):
# print("�������� ��������: x")
# del self.__x
#
# def __del_coord_y(self):
# print("�������� ��������: y")
# del self.__y
#
# coord_x = property(__get_x, __set_x, __del_coord_x)
# coord_y = property(__get_y, __set_y, __del_coord_y)
#
#
# pt = Point()
# print(pt.__dict__)
#
# pt.coord_x = 100 # ������ �������
# x = pt.coord_x # ������ �������
# print(pt.__dict__)
# print(x)
#
# pt.coord_y = 50
# y = pt.coord_y
# print(pt.__dict__)
# print(y)
#
# del pt.coord_x # ������� ��������� �������� __x
# print(pt.__dict__)
# pt.coord_x = 7 # ������� ��������� �������� __x
# x = pt.coord_x # ������� ��������� �������� __x
# print(pt.__dict__)

# ��������:
# ������� � ������ class Point
# �������� coord_x � ������� ����� ��������, ��� � ������� ����������:
# (������������ ��������� �������, ��� � ������� ����������)
# ���������� ������� � ��������� pt.coord_x = 100
# ��������� ������ ������ x = pt.coord_x
# � ������������� ����������� �������� ������������ ��������� ������
# � �������� � �������� � ������ ������ __check_value(x)
# �� ����� ������������ �������� ��������� � ���������,
# �� ������� �� ���������� (��� �����������)
# �.�. ������ �� �� ����� ��������� � ��� ��� pt.get(x)
# � ������ ������� ������ class Point
# �������� ����� property, �������� �������� coord_x.
# ��� �������� ����� ������������ ���� ������� � �������
# coord_x = property(__get_x, __set_x)
# ������ � ����� ������: ������� ������ ����� ������ ��� ����� ������
# �.�. ������: pt.coord_x = 100 ����� ��������� ��������� ������ __set_x
# �.�. ������: x = pt.coord_x ����� ��������� ��������� ������ __get_x
# coord_y = property(__get_y, __set_y)
# �.�. ������: pt.coord_y = 100 ����� ��������� ��������� ������ __set_y
# �.�. ������: x = pt.coord_y ����� ��������� ��������� ������ __get_y
# ��������� �������� coord_x ����� �� ������ ������ � ���������� �������,
# �� � ������ �������:
# �� ����� ������� ����� __del_coord_x()
# � �������� coord_x ������� ������� ��������� __del_coord_x
# coord_x = property(__get_x, __set_x, __del_coord_x)
# ��� ���������� �������� del pt.coord_x
# ������� �������� ��������� __x �� ��������� ������ pt, � �� ��� ������ coord_x
# � ������� ��������� �������� ������������� ���������� x = pt.coord_x
# ������� ������ AttributeError: 'Point' object has no attribute '_Point__x'
#
#
# ��������� ����� ���������� ������-������� (������-�������� coord_x / ������-
�������� coord_y)
#
#
# ������� ��� � ������
# class Point:
# def __init__(self, x=0, y=0):
# self.__x = x
# self.__y = y
#
# def __check_value(a):
# if isinstance(a, int) or isinstance(a, float):
# return True
# return False
#
# @property
# def coord_x(self):
# print("����� get_x")
# return self.__x
#
# @coord_x.setter
# def coord_x(self, x):
# print("����� set_x")
# if Point.__check_value(x):
# self.__x = x
# else:
# raise ValueError("�������� ������ ������: x")
#
# @coord_x.deleter
# def coord_x(self):
# print("�������� ��������: x")
# del self.__x
#
# @property
# def coord_y(self):
# print("����� get_y")
# return self.__y
#
# @coord_y.setter
# def coord_y(self, y):
# print("����� set_y")
# if Point.__check_value(y):
# self.__y = y
# else:
# raise ValueError("�������� ������ ������: y")
#
# @coord_y.deleter
# def coord_y(self):
# print("�������� ��������: y")
# del self.__y
#
#
# pt = Point()
# print(pt.__dict__) # {'_Point__x': 0, '_Point__y': 0}
# pt.coord_y = 100
# print(pt.__dict__) # {'_Point__x': 0, '_Point__y': 100}
# del pt.coord_y # ������� ��������� �������� __y
# print(pt.__dict__) # {'_Point__x': 0}
# pt.coord_y = 7
# print(pt.__dict__) # {'_Point__x': 0, '_Point__y': 7}
# �� �������� ����� �����
#
#
#
# �����������
#
# � �������� ������� ������� ��������������� DRY (Don't Repeat Yourself) = ��
��������,
# � ���� �� ���������� �������� __y ��������� ����� �� ������, ��� � �� ����������
�������� __x
# �� ����� � ��������� �����������
# ���������� - ��� ����� � ����������� ���� ������
# � ������� ����������� ����������� ������ __get__(), __set__(), __del__()
# ��������� ���� � ������ �����������
# (� ����������� ������ ���� ������� �������� �� ��������)
#
# class CoordValue:
# def __get__(self, instance, owner):
# return self.value
#
# def __set__(self, instance, value):
# self.value = value
#
# def __delete__(self, obj):
# del self.value
#
#
# class Point:
# coord_x = CoordValue() # coord_x - ��� � ���� ����������
# coord_y = CoordValue() # coord_y - ��� � ���� ����������
#
# def __init__(self, x=0, y=0):
# self.coord_x = x # ����������� coord_x ������������ ���������� ��������
x
# self.coord_y = y # ����������� coord_y ������������ ���������� ��������
x

# pt = Point(1, 2)
# print(pt.__dir__())
# print(pt.coord_x, pt.coord_y)
# pt.coord_x = 5 # ������������ ���������� ��� ������� ��������
# print(pt.coord_x, pt.coord_y)
# # hasattr(obj, name) = �������� �� ������� �������� name � obj
# print(hasattr(pt, 'coord_x')) # True
# del pt.coord_x # ������� ��������� �������� __x
# # hasattr(obj, name) = �������� �� ������� �������� name � obj
# print(hasattr(pt, 'coord_x')) # False
# pt.coord_x = 7
# ��� ��� ��������:
#
# ���� ����� class CoordValue (����������)
# � ������ ����� ������-����������� � ������ class Point
# ������ ��� ���������
# coord_x = CoordValue() # coord_x - ��� � ���� ����������.
# coord_y = CoordValue() # coord_y - ��� � ���� ����������.
# � ������ ��������� ������� ��������� value (� ������ ���)
# � ������ �������� �������������� ����������.
# ����� �� ������ �������� ������ pt = Point(1, 2)
# �������� ����������� def __init__(self, x=0, y=0)
# �������� 1 ��������� � value �� coord_x
# �������� 2 ��������� � value �� coord_y
# �����, ����� �� ����� �������� ������ pt ��������� � �����������
print(pt.coord_x, pt.coord_y)
# �� ���������� �� ��� ���� ��������������� �� ������ class Point coord_x �
coord_y
# �.�. ���������� ��������� �� ������� ������� value
# � ���� ����� ��������
# print(id(Point.coord_x), id(pt.coord_x))
# 1626515472 1626515472 - ��� ���� � �� �� �����
# ������ ��� ����������, ���� �� ���������� self � ������������
# self.coord_x = x
# self.coord_y = y
# ������ �� ������� �������� � ��������� ������ pt ?
# ���� � ���, ��� � ������������ ������� ����������� self.coord_x = x
# ������ ������������� � � ����������������, ����� ����������� ������������,
# ��������� ����� class CoordValue: def __set__(self, instance, value)
# ���� ����� ������������: ���������� �������� � � value �� coord_x
# � �� ���� �� ����������, ������� ��������� ������� � pt �� �������
# ��� �������� � �� ���������� ������
# ��� �������� ���������� ���������� ������
# pt1 = Point(1, 2)
# pt2 = Point(10, 20)
# print(pt1.coord_x, pt1.coord_y) # 10 20
# print(pt2.coord_x, pt2.coord_y) # 10 20
# �������� ���������, �.�. ��� ������� �� ���� � �� �� ������� value
# ������� ������ �� ������ class Point
#
#
# �������� ��� (������ ���� ������� �������� �� ��������)
# ����� ���������� ����� ��������� ��������������� � ���������� ������
# �� ����� ����� ��������� � ���������� instance
# ��� ����� �������� �� ��� �������� ������ �� �������� ������ ����������
# � �����, �������� �������� __dict__
# ��� ������� �������� [self.__name]
# � ��������� ����������� �������� = value
# ��� �� __set__ instance.__dict__[self.__name] = value
#
# �� __get__ ����� ��������� � ���������� instance
# ������ �������� �� ��� �������� ������ �� �������� ������ ����������
# �������� �������� __dict__
# ���� ������ �������� [self.__name]
#
# �� ��������� ����� �������� self.__name
# �������� ����������� __init__
#
#
#
#
# class CoordValue:
# def __init__(self, name):
# self.__name = name
#
# def __get__(self, instance, owner):
# return instance.__dict__[self.__name]
#
# def __set__(self, instance, value):
# instance.__dict__[self.__name] = value
#
#
# class Point:
# coord_x = CoordValue("coordX")
# coord_y = CoordValue("coordY")
#
# def __init__(self, x=0, y=0):
# self.coord_x = x
# self.coord_y = y
#
#
# pt1 = Point(1, 2)
# pt2 = Point(10, 20)
# print(pt1.coordX, pt1.coordY) # 1 2
# print(pt1.__dict__) # {'coordX': 1, 'coordY': 2}
# print(pt2.coord_x, pt2.coord_y) # 10 20
# print(pt2.__dict__) # {'coordX': 10, 'coordY': 20}
# ��������� instance �������� �� ��� �������� ������ �� �������� ������ ����������
# � ��������� __dict__ ����� ������ �������� self.__name
# �� ��������� ���������� �������� self.__name �������� ����������� def
__init__(self, name)
# � ������, ����� � ������ class Point
# �������� coord_x = CoordValue( ) ���������� �������� ��������
# �� ����� ��������� ���������� coord_x = CoordValue("coordX")
# ������ � ������ ��������� ������ ������� ���� coordX � coordY
#
# ������� ���: ������ � ������ Python 3.6+
# � ������������ ������ ����� __set_name__(self, owner, name)
# ����� ��������� ������������� ��� �������� ����������� CoordValue()
# � � ��������� name ������� �� ��������� ������
# ��������� ���:
# ������ ������������ def __init__(self, name)
# �������� def __set_name__(self, owner, name)
# � � ������ coord_x = CoordValue("coordX")
# ����� "coordX"
#
# class NonDataDescr:
# def __set_name__(self, owner, name):
# self.__name = name
#
# def __get__(self, instance, owner):
# return "NonDataDescr __get__"
#
#
# class CoordValue:
# def __set_name__(self, owner, name):
# print(name)
# self.__name = name
#
# def __get__(self, instance, owner):
# return instance.__dict__[self.__name]
#
# def __set__(self, instance, value):
# instance.__dict__[self.__name] = value
#
#
# class Point:
# no_data = NonDataDescr()
# __slots__ = ['__x', '__y', '__dict__']
#
# coord_x = CoordValue()
# coord_y = CoordValue()
#
# def __init__(self, x=0, y=0):
# self.coord_x = x
# self.coord_y = y
#
#
# pt1 = Point(1, 2)
# pt2 = Point(10, 20)
# print(pt1.coord_x, pt1.coord_y) # 1 2
# print(pt1.__dict__) # {'coord_x': 1, 'coord_y': 2}
# print(pt2.coord_x, pt2.coord_y) # 10 20
# print(pt2.__dict__) # {'coord_x': 10, 'coord_y': 20}
# print(pt1.__dir__())
# ���� � ������ �� ����������� ��������� � ������ ��������(����������),
# �� ��������� ������ ������ ����������� - ��� Python �������� ����������.
# � Python ���� ����������� ���� �����
# ����������� ������ (data descriptor)
# ����������� �� ������ (non-data descriptor)
# ������ class NonDataDescr - ���������� ������ ���� ����� def __get__(self,
instance, owner)
# � ������ class Point ������ ����� ���������� no_data = NonDataDescr()
# ���� ������ ����������� ���-�� ��������
# print(pt1.no_data) # NonDataDescr __get__
# pt1.no_data = "hello"
# print(pt1.no_data) # hello
# ������ �� �����, �� ���������� no_data �������.
# ����� ������� ����� �������� no_data �� ��������� hello
# � ����� �������� � no_data ����� ��� �� �������, � �� ��� � ������������.
# ���������� �� ������ ����� ������������ ������ �� ���� ������.
# � �������� ��������� ����������� ������ (data descriptor).
# ����������� ������� ����� (�������� ��� � �.�.),
# �� ���� ����� ����� (� ������ �������� � ��������) ����������� ���,
# �� �� ����� � ������ �������� (�� �������)
#
#
=======================================
# ���������� ����� __call__
# dunder-������(�� ����.��������� double underscore)
#
# ����, ����� ��������� ����� __call__ � �� ���� �� �����?
#
# C������� ����� ������ class Counter � �������-��������������� __init__
# � ���� �������������� � ���������� ������ ����� ���������
# ��������� �������� __counter = 0
# class Counter:
# def __init__(self):
# self.__counter = 0
#
# �������� �������� �������
# c = Counter()
# �������� �������� �� ������� ������
# � ����� ������ ��� �������� ������ �������
# ��� ����� ��� ����� �������� � ������
# � ����������������, ����� ���������� ����� ������,
# �� ������������� ���������� ���������� ����� __call__
# � � ������ ������ �� ������� ����� �������� ����� ������:
# __call__(self,*args,**kwargs):
# obj = self.__new__(self,*args,**kwargs)
# self.__init__(obj,*args,**kwargs)
# return obj
# ��� ����� ��������� ����� ���������� ������ __call__,
# � ����������������, ��� ��������� �������, �� ������� ��� ��:
# ������� ��������� ���������� ����� __new__
# �� ������� ������ ������� � ����� ����������,
# � �����, ����� __init__ - �� ��� �������������.
# �� ����, ����� ����� �������� ������� �������
# �������� ���������� �� ���� ���������� ����������� ������ __call__.
# � ��� ��������� ������� ��� �������� ��� �����.
# � ��� ��������� ������� ��� �������� ��� �����.
# ���� �������� �������,�� ��������� ������.
# c() # TypeError: 'Counter' object is not callable
# ��� �� ��� ����������, �� ����� ��������� ���� ������,
# ���� ���� � ������ Counter �������� ���������� ����� __call__,
# ��������, ���:
# class Counter:
# def __init__(self):
# self.__counter = 0
#
# def __call__(self,*args,**kwargs):
# print('__call__')
# self.__counter += 1
# return self.__counter
#
# ����� �� ������� ���������, ��� ��� ������ ������ �����,
# ����� ����������� ������� counter �� �������� ������� �� 1
# � ���������� ���.
# �������� ��������� ����� ����������� ������ � ��� �����, ������
# ����� �������� ��� ��������� ������� �������
# ����� �������� ������� ������.
# c = Counter()
# c2 = Counter()
# c()
# c()
# res = c()
# res2 = c2()
# print(res,res2)
# ������� ��� �������-�������
# � ��� ����� �������� ���������� ����������
# � ������������ ����� ����������� �������.
# ������� ��� ��� ��������� �� ����������� ������ __call__.
# ����� �������� ��������� *args, **kwargs.
# ��� ������, ��� ��� ������ �������� �� ����� ���������� ��
# ������������ ���������� ����������.
# ��������, � ����� ������ ����� �������
# �������� �������� �������� (��� �������� �������)
# ��� ������� ������.
# �� ����� � �������� ����� __call__, �������� �������:
# class Counter:
# def __init__(self):
# self.__counter = 0
#
# def __call__(self,step=1,*args,**kwargs):
# print('__call__')
# self.__counter += step
# return self.__counter
#
# c = Counter()
# c2 = Counter()
# c()
# c(2)
# res = c(10)
# res2 = c2(-5)
# # print(res,res2) # 13 -5
# ��� ����� ������� ������ ����������� ������ __call__.
#
#
# ������ ��������� ����� _call__, ���� �� �� ��������� � ������?
# � ��� ������ �� �������� object.
# �� ���� �� ��� ������������� � ����� ������, ��� � ��� ��������,
# �� ������ ����� �� ������� ���������,
# ���������� __call_ �� ������ � ������� _new_ ���
# � �������������� �������� �� ����� ������.
# ��� �� �� ��������, ��� ����� _new_ ���������� ������ ��� �������� ���������,
# ����� ����� ��� �������� �� __call__, ��� �������� � ��� � �������?
#
# ��, � ��� ������� ������.
# ����� ���� ���� ����.
# ����� �� ��������� ����� _call__,
# �� �� ��������� ������ �� ���������� ������, �� �� �� ������ ������.
# �������, ����� �� ������� ������� ������, ��������, pt = Point(),
# �� ����� ��������� __call_ �� ������,
# ������� ��������� � ���������� type (� ����������� � ��� ���� ������������).
# ������, ������ ����� ������� )
#
#
#
#
# �� ����� �������,���� ������ ������:
# ����� ��� �����, ��� ����� ����������?
#
# ������ ������ � ��� ������������� ������ � ������� __call__
# ������ ��������� �������.
#
# �� ����� ������� ����� StripChars,
# ������� �� ����� ������� � � ����� ������ �������� �������:
# class StripChars:
# def __init__(self,chars):
# self.__chars = chars
#
# def __call__(self,*args,**kwargs):
# if not isinstance(args[0],str):
# raise ValueError('�������� ������ ���� �������')
# return args[0].strip(self.__chars)
#
# �� �����, � �������������� �� �������� ������ __chars � �������� �������,
# � �����, ��� ������ ������ __call__ ������ �������
# ����� ��������� ����� strip �� �������� __chars.
# �� ����, ������ ����� ������� �������� ������ � ������� �� �������,
# ������� ������� �������:
# s1 = StripChars(" ?:!.;")
# s2 = StripChars(" ")
# res1 = s1(" Hellow world:?! ")
# res2 = s2(" Hellow world:?! ")
# print(res1,res2, sep='\n')
# � ����������
# ������ s1 ����� ��������
# �� �������� ��������� �������� � ������ � ����� ������.
# ������ s2 ����� ��������
# �� �������� ������ �������� � ������ � ����� ������.
#
# ������ ������ � ��� ��������� ����������� � ������ �������.
#
# �������� �����-���������, ��������� �������� ��������
# ����������� ����������� ������� � ����� ����� �.
# class Derivate:
# def __init__(self, func):
# self.__fn = func
#
# def __call__(self, x, dx=0.0001, *args, **kwargs):
# return(self, __fn(x + dx) - self.__fn(x)) / dx
# � __init__ ������� ������ func ������ ����� ��������
# � ��������� ������ __fn, ������ ����� �������� �� ������ func
# � __call__ �������� ��� ����������:
# ����� � � ������� ����� �������� ����������
# dx=0.0001 ��� ��� �������� ������� �� ��������� �����������
# � ���������� ���������� ��������.
#
# ��������� ������ �� ������� ��� ���������� ����� ��������
# �������� ������ ������, ������ ���������� �������� ������ � ����� �
# def df_sin(x):
# return math.sin(x)
# � ������ �� ���� ��� ������������:
# print(df_sin(math.pi/4))
# �� ������ ���������� ������������� ������ math
# �������� � ������� ���������
# ������ ������:
# df_sin = Derivate(df_sin)
# ������ df_sin � ��� �������� ������ Derivate, � �� ������� ������.
# print(df_sin(math.pi/3)) # 0.49995669789693054
# ������ ������ � ��� @Derivate ����� ���������� �������
# ��� ������, ��� ���������� ������ � ���������� ������ �� ������,
# � �����, �������� �� ���������� � ���������� ������ __call__.
# ������ �� ������
# import math
# class Derivate:
# def __init__(self, func):
# self.__fn = func
#
# def __call__(self, x, dx=0.0001, *args, **kwargs):
# return (self.__fn(x + dx) - self.__fn(x)) / dx
#
# @Derivate
# def df_sin(x):
# return math.sin(x)
#
# print(df_sin(math.pi/3)) # 0.49995669789693054
# �������, ��� ����� ��� ������� �������,
# ����� ������������������ ������� ��� ������.
# � �������� �������, ��������, �� ������ ����,
# �������� ���� �����, ������, ����� ���������, ������
# ������� �������� �� ������ ������� �����.
# ��� ��� ����� ��������������, ������� ������ ������� ������ �����������.
# � ������������� ��, � ��������, �� ������� ������������ �����.
# #
=======================================
# ���������� ����� __str__ ���������� ����� __repr__
#
# ������ ���������� ����� ����� ���� ����������
# � ������������� �����������
# � ������������ ������ �������.
#
# �������� ��� ��� ������ ������� �� ��������� �����������:
#
# __str__() � ���������� ����� �� ���������� ����������
# �� ������� ������ �� ������������� (��������, �� ������� print, str);
#
# __repr__() � ���������� ����� �� ���������� ����������
# �� ������� ������ � ������ ������� (�� �������������).
#
# ���� �� �������� ����� __str__
# �� ������������� ����� ������ __repr__
# � �� �����������

class Cat:
def __init__(self, name):
self.name = name

def __repr__(self):
return f"{self.__class__}: {self.name}"
def __str__(self):
return f"Cat: {self.name}"

c = Cat('Vaska')
# ����� ��� ���������� �������
print(c) # <__main__.Cat object at 0x02BEA1C0>
# ��������� __repr__ (�� �������������)
# ����������� �� ������ � ������� ��� �������
print(c) # <class '__main__.Cat'>: Vaska
# ��������� __str__ (�� �������������)
print(c) # Cat: Vaska
# #
=======================================
# ���������� ����� __len__ ���������� ����� __abs__
#
# ������ ���������� ����� ����� ���� ����������
# � ������������� �����������
# � ������������ ������ �������.
#
#
# __len__() � �������� �������� ������ len() � ���������� ������;
#
# __abs__() - �������� �������� ������ abs() � ���������� ������.
#
# ���� ����� Point, ������� ����� ������� ������������ ������ ��������� �����:
class Point:
def __init__(self, *args):
self.__coord = args

def __len__(self):
return len(self.__coord)

def __abs__(self):
return list( map(abs, self.__coord) )

p = Point(1, 2)
# ����� ��� �������������� __len__
# print(len(p)) # TypeError: object of type 'Point' has no len()
# ������ ������, ��� ��� ������ len �� ���������
# � ���������� ������� �� ��������.
# ����� �������� ��� ���������, ����� �������������� ���������� ����� __len__
print(len(p)) # 2 ���������� ��������� �����
# ��� ��� ������ abs �� ��������� � ���������� ������� �� ��������.
# �� ������������ abs(), ����� �������������� ���������� ����� __abc__
# ����� �������� ������ �� ������ ����������
# �.�. __abc__ ����� ���������� ������ �� ������� ���������
# �������� ������ abs() � ������� �������� ��������� __coord
p1 = Point(1, 3, -5)
print(abs(p1)) # [1, 3, 5]
# #
=======================================
# ���������� ������ �������������� ��������
#
# __add__() � �� �������� �������;
# __sub__() � �� �������� ��������;
# __mul__() � �� �������� ��������;
# __truediv__() � �� �������� ������.
#
# �������� ������ ���� ������� ����� ����� �� ���������� �������.
# �����������, ��� �� ������� ����� �� ������ �� ��������.
# ��� ��������� ����� ������� ����, ������ � ������� �������� ��.
# ��������� ������ ������� ����� 00:00 ����� ����.
# �.�. 12 ����� ���� - ��� 0 (����), � ������ 1,2,3,4 � �.�.
# ���� ����� ������� � ���� ������
# � ������������ ��������� 86400 � ����� ������ � ����� ���.
# ������� ����� ����������� � ��������������
# ����� ����� ������� �� ������ �� ��� ��������.
# ��� ������� ����� ���������� ������ � ���,
# � ����� ��� ����� ���� ������ � ��� ����
# �������������� ��������� ������ __DAY = 86400
# �� � ���������� ����� ������� �� ��������� ��� ��������
# ����� ������ � ����� ��� � �� ����� ����������
# ������� ������� �� ������ self.seconds = seconds % self.__DAY
# �������� �������� ��� ������� �������� seconds.
# ����� ���� ����� ��������� � ������ ��������� ��� ������.
# ��� ������ ����� Python ������������ ������������,
# ����� ��� ������ ������� ���������� � �������� seconds.
# �������, �� ����� ���������� � ������ ���� ������,
# �������� ���������� ����� ���, ��� ���� ������� �� ������������ � �� �����.
# ������� ����� ������ �������������� �� ������ ��������,
# ��� �������� seconds ������ ������ ����� ������.
# ���� ��� �� ���, ���������� ��������� TypeError.
# ����� � ���� �� ������ �������� ����� get_time
# �� �������� �������� ������� � ���� ��������� ������.
# � ���� ������ ����� ������� ���������� ������� ������, �����, �����.
#
# ��������� s ����� ������� ������� ���������� ������
# �� ����� �������� ������� �� ������ �� 60
# (� ������ 60 ������, ������� �� ������ � ���� ������� �������)
# s = self.seconds % 60 # �������
#
# ��������� m ����� ������� ������� ���������� �����
# �� ����� ������� ������� �������������� ������� �� 60
# (������� ��������� � ������)
# � ����� �������� �������� �� ������ �� 60
#(� ���� 60 �����, ������� �� ������ �� 60 � ���� ������� ������)
# m = (self.seconds // 60) % 60 # ������
#
# ��������� h ����� ������� ������� ��������� �����
# �� ����� ������� ������� ������������� ������� �� 3600
# (��������� ������ � ����� ����)
# � ����� �������� ������� �� ������ �� 24
# (� ������ 24 ����, ������� �� ������ �� 24 � ���� ������� ����)
# h = (self.seconds // 3600) % 24 # ����
#
# � ��������� �������,������,���� ����� � ����������������� ����
# � ������ ������ __get_form()
#
# ����� ������������� ��������� ����� ������ __get_form
# �� ������������� ������� str(x).rjust(2, "0")
#(��������� ���������� ������ ����, ���� ����� ������ 10).
class Clock:
__DAY = 86400 # ����� ������ � ����� ���

def __init__(self, seconds: int):


if not isinstance(seconds, int):
raise TypeError('������� ������ ���� ������')
self.seconds = seconds % self.__DAY

def get_time(self):
s = self.seconds % 60 # �������
m = (self.seconds // 60) % 60 # ������
h = (self.seconds // 3600) % 24 # ����
return f'{self.__get_form(h)}:{self.__get_form(m)}:{self.__get_form(h)}'

@classmethod
def __get_form(cls,x):
return str(x).rjust(2, '0')

# def __add__(self, other):


# if not isinstance(other, int):
# raise ArithmeticError("������ ������� ������ ���� ����� int")
# return Clock(self.seconds + other)

# ��������� �������� � Python


# res = code_true condition code_false
# ��������� res
# ���� ������� condition==true ��������� �������� code_true
# ���� ������� condition==false ��������� �������� code_fase
#
# ����������� ������
# if condition:
# res = code_true
# else:
# res = code_false
#
# ������
# sc = other if isinstance(other, int) else other.seconds
# ���������� sc ��������� other
# ���� ������� if isinstance(other, int) ������ (true)
# � ��������� ������
# ���������� sc �������� other.seconds
#
#
def __add__(self, other):
# print('__add__')
if not isinstance(other, (int, Clock)):
raise ArithmeticError("������ ������� ������ ���� ����� int ���
�������� Clock")
#sc = other if isinstance(other, int) else other.seconds
sc = other
if isinstance(other, Clock):
sc = other.seconds
return Clock(self.seconds + sc)

#
# � ������ __radd__ ���������� ������ ���� ������ �������� ������ �� +
# �� ������ ��������� ��� ������
# ���� ����� �������� ������ �� +
# � ������ return self + other
# ��� self ��� ������ c + other ��� 100
# � ���������� ��������:
# ����� ����� �������� �������� self + other
# �������� ����� __add__
# ������� ���������� ����� ������ return Clock(self.seconds + sc)
# � ��� ����� __radd__ ����� ���
#
#
def __radd__(self, other):
# print('__radd__')
return self + other

# ����� __iadd__ ���� � ���� �������������� ��������


# ������� �������� ���������: += ��� -= ��� *= ��� /= � �.�.
# �� ���� ����� ����������� ������� +=
# �� ������� ����� �������� ������ Clock
# � ����� ������ �� �����
# � ����� ������ �������� �������� ��� ��������� ���������� seconds
# ������� � ����� � ������ ���������� ���������� ����� __iadd__
# ������ ����������� ��������:
# ���� other �� ����� ���� int � �� ������ ������ Clock
# ������� ������
# ������ ����������� ���������� ������ sc
# ������� ����� ��������
# ������� ����������� � ������� ��������� ������ self.seconds
# ����������� �� ���������� ������ sc self.seconds += sc
# � ���������� ������� ������
# �.�. �� ����� �� ������ ����� �������� ������
# � � ������� ��������� ������ ����������� ���������� ������ �� 100
#
#
def __iadd__(self, other):
# print('__iadd__')
if not isinstance(other, (int, Clock)):
raise ArithmeticError("������ ������� ������ ���� ����� int ���
�������� Clock")
sc = other
if isinstance(other, Clock):
sc = other.seconds

self.seconds += sc
return self

# c1 = Clock(1165546)
# print(c1.get_time()) # 11:45:11
# ������ �������� ���� �� 100
# c1.seconds = c1.seconds + 100
# print(c1.get_time()) # 11:47:11
# c2 = Clock(10)
# print(c2.get_time()) # 00:00:00
# c2 = Clock(20)
# print(c2.get_time()) # 00:00:00
# c2 = Clock(30)
# print(c2.get_time()) # 00:00:00
# c2 = Clock(40)
# print(c2.get_time()) # 00:00:00
# c2 = Clock(50)
# print(c2.get_time()) # 00:00:00
# c2 = Clock(60)
# print(c2.get_time()) # 00:01:00
# c2 = Clock(120)
# print(c2.get_time()) # 00:02:00
# c2 = Clock(240)
# print(c2.get_time()) # 00:04:00
# c2 = Clock(480)
# print(c2.get_time()) # 00:08:00
# c2 = Clock(960)
# print(c2.get_time()) # 00:16:00
# c2 = Clock(1000)
# print(c2.get_time()) # 00:16:00
# c2 = Clock(1100)
# print(c2.get_time()) # 00:18:00
# c2 = Clock(1200)
# print(c2.get_time()) # 00:20:00
# print()
# c = Clock(1000)
# print(c.get_time()) # 00:16:00
# ������ �������� ���� �� 100
# c.seconds = c.seconds + 100
# print(c.get_time()) # 00:18:00
# ���� �������� ��� c = c + 100
# ������� ������, ������ ��� �������� +
# �� �������� � ���������� ������ c
# ��� ����� ��������� ������� � ������
# ���������� ����� __add__ (self, other)
# othet - ��� �������� ������ �� ��������� + (100)
# self - ��� �������� ����� �� ��������� + (�)������ �� �������� ������
# �������� ����� �������� ������ Clock
# ������� �������� �������� ������ ������� ����
# ��� �������� ������ ������ �������� ������ �� ��������� +
# return Clock(self.seconds + other)
# �� ������ ��������� �������� other �� ����� �����
# � ������ ��� ���������
# c = c + 100
# print(c.get_time()) # 00:20:00
#
#
# �������� ���������� ������ __add__
# ����� ����� ���� ���������� � ������� c = �1 + c2
# �� ����� ������������� ����� __add__
# ����� int ����� ������������ ��������� ������ Clock
# if not isinstance(other, (int, Clock)):
# �������������� ��������� sc �������� �� other
# sc = other
# ����� ��������:
# if isinstance(other, Clock):
# sc = other.seconds
# ��� �������� ����� �������
# sc = other if isinstance(other, int) else other.seconds
#
#
# c1 = Clock(1000)
# c2 = Clock(2000)
# c = c1 + c2
# print(c.get_time()) # 00:50:00
# ����� ������� ������ ���������
# c3 = Clock(3000)
# c = c1 + c2 + c3
# print(c.get_time()) # 01:40:01
#
#
# ������ ������ �����:
# � = � + 100 ��������
# � = 100 + � ������
# c += 100 ������
#
# ���� ������: � = 100 + �
# ����� ��������� �������� ����������� ������ __radd__
# � ����� ��� ����� ��������
# c = Clock(1000)
# print(c.get_time()) # 00:16:00
# c = 100 + c
# print(c.get_time()) # 00:18:00
#
# ���� ������: c += 100
# ����� ��������� �������� ����������� ������ __iadd__
# � ����� ��� ����� ��������
# c = Clock(1000)
# print(c.get_time()) # 00:16:00
# c += 100
# print(c.get_time()) # 00:18:00
#
#

#
#
# �������� ����������� ������ ������ ��������������� ����������� ������ __add__() �
��� ��������� __radd__() � __iadd__().
# �� �������� ���������� � ��� ��������� �������� ���������� ������:
#
#
# �������� ����� ��������� �������� ����� ���������
#
# x + y __add__(self, other) x += y
__iadd__(self, other)
# x - y __sub__(self, other) x -= y
__isub__(self, other)
# x * y __mul__(self, other) x *= y
__imul__(self, other)
# x / y __truediv__(self, other) x /= y
__itruediv__(self, other)
# x // y __floordiv__(self, other) x //= y
__ifloordiv__(self, other)
# x % y __mod__(self, other) x %= y __imod__(self,
other)
#
#
# #
# �� ������ :
#
class BankAccount:
def __init__(self, name, balance):
self.name = name
self.balance = balance

def __add__(self, other):


print('__add__')
if isinstance(other, BankAccount):
print(f'other = {other}')
return self.balance + other.balance
if isinstance(other, (int, float)):
print(f'other = {other}')
return self.balance + other
raise NotImplemented('����� ������� �� �������������')

def __radd__(self, other):


print('__radd__')
return self + other

def pb(self):
print(f' name: {self.name} \n balance: {self.balance}')

b = BankAccount('Ivan', 900)
b.pb() # 900
print()
b.balance += 400
b.pb() # 1300 ��������� ������� ��� ���� ������ �������
print()
# �� ���� ������ ����������� ������� ��������� �����
# print(b + 200) ������� ������:
# Python �������� ����� __add__
# b.__add__(200)
# � ����� ���������� � ����� ������ ���
# TypeError: unsupported operand type(s) for +: 'BankAccount' and 'int'
# ��������� � ������ ��������� ����� __add__(self, other):
# self ��� ������ �� ������ b
# other ��� ����� ������� ��������� 200
# �������� ��������:
# ���� other ��� int ��� float
# ���� � ������ ������� ���� self.balance
# � � ���� ��������� other
# ������ ��� ��������
b.balance = b + 200 # 1500
b.pb()
print()
# ������ � �������� �����
# ���� ����������� �������
# print(200 + b) ������� ������:
# Python �������� ����� __add__
# 200.__add__(b) �� ���������,
# ����� ������� � �� �������� ����� __radd__
# b.__radd__(200)
# � ����� ���������� � ����� ������ ���
# TypeError: unsupported operand type(s) for +: 'int' and 'BankAccount'
# ��������� � ������ ��������� ����� __radd__(self, other):
# � ������ self ������� ����� �� +
# � other ������� ������ �� +
# ������ ��� ��������
b.balance = 200 + b # 1700
b.pb()
print()
#
#
print()
# �������� �� ���� ������
k = BankAccount('Kat', 300)
k.pb()
# ��������� ������� ����� ����� ��������
# print(b + k)������� None:
# ����� __add__ �� ��������� �.�. �� ����������� �������� ���� ��������
# ��������� � ������ __add__(self, other): ���� �������
# self ��� ������ �� ������ b
# other ��� ������ �� ������ c
# �������� ��������:
# ���� other ��� ����� BankAccount
# (�.�. ������ � � �� ������� ������� ��� ��������)
# ���� � ������ ������� ���� self.balance (������ ��� b)
# � � ���� ��������� other.balance (������ ��� c)
# �� ���� ������ �����: �����, ������� � �.�.
# ����� �������� ���������
# raise NotImplemented('����� ������� �� �������������')
# ������ ��� ��������
k.balance = b + k # 2000
k.pb()
#
#
# ������� ��������� __add__(self, other): ������ ���������� ����� ������
#
class BankAccount:

def __init__(self, name, balance):


self.name = name
self.balance = balance

def __repr__(self):
return f' --- \n name: {self.name} \n balance: {self.balance} \n ---'

def __add__(self, other):


print('__add__')
if isinstance(other, BankAccount):
return BankAccount(self.name, self.balance + other.balance)
if isinstance(other, (int, float)):
return BankAccount(self.name, self.balance + other)
raise NotImplemented('����� ������� �� �������������')

def __radd__(self, other):


print('__radd__')
return self + other

b = BankAccount('Ivan', 900)
print(b)
b = b + 400
print(b)
b = 400 + b
print(b)
k = BankAccount('Kat', 200)
print(k)
k = k + b
print(k)
print(b)

����� � �������:
---
name: Ivan
balance: 900
---
__add__
---
name: Ivan
balance: 1300
---
__radd__
__add__
---
name: Ivan
balance: 1700
---
---
name: Kat
balance: 200
---
__add__
---
name: Kat
balance: 1900
---
---
name: Ivan
balance: 1700
---
#
#
=======================================
# ==========================================================#
==========================================================
# 1. �������� ��� ���������� ������ � ����� Clock ( �� �������� � ��������
__add__() ).
# ��� ���� ��������� �������� �� �������������� ����������� ����.
# ��� ����������, ���� �� �� ��� ������ ����������� ������ ��������������
���������� ������.
# ���������, ��� ��� ������� ���������.

# ���������� ������ �������������� ��������


#
# __add__() � �� �������� �������;
# __sub__() � �� �������� ��������;
# __mul__() � �� �������� ��������;
# __truediv__() � �� �������� ������.
#

class Clock:
__DAY = 86400 # ����� ������ � ����� ���

def __init__(self, seconds: int):


if not isinstance(seconds, int):
raise TypeError('������� ������ ���� ������')
self.seconds = seconds % self.__DAY

# ����� ��������� ��������� ��� ��������� � print() / print(c)


def __repr__(self):
s = self.seconds % 60 # �������
m = (self.seconds // 60) % 60 # ������
h = (self.seconds // 3600) % 24 # ����
return f'{self.__form(h)}:{self.__form(m)}:{self.__form(h)}'

@classmethod
def __form(cls,x):
return str(x).rjust(2, '0')

# x + y ---------------------------------------
def __add__(self, other):
print('\t __add__')
print(f'\t self = {self}')
print(f'\t other = {other}')
if not isinstance(other, (int, Clock)):
raise ArithmeticError("������ ������� ������ ���� ����� int ���
�������� Clock")
#sc = other if isinstance(other, int) else other.seconds
sc = other
if isinstance(other, Clock):
sc = other.seconds
print(f'\t sc = {sc}')
return Clock(self.seconds + sc)
def __radd__(self, other):
print('\t __radd__')
print(f'\t self = {self}')
print(f'\t other = {other}')
return self + other

def __iadd__(self, other):


print('\t __iadd__')
print(f'\t self = {self}')
print(f'\t other = {other}')
if not isinstance(other, (int, Clock)):
raise ArithmeticError("������ ������� ������ ���� ����� int ���
�������� Clock")
sc = other
if isinstance(other, Clock):
sc = other.seconds
print(f'\t sc = {sc}')
self.seconds += sc
return self

# x - y ---------------------------------------
# ��������� �������� � Python [when_true] if [condition] else [when_false]
def __sub__(self, other):
print('\t __sub__')
print(f'\t self = {self}')
print(f'\t other = {other}')
if not isinstance(other, (int, Clock)):
raise ArithmeticError("������ ������� ������ ���� ����� int ���
�������� Clock")
sc = other if isinstance(other, int) else other.seconds
print(f'\t sc = {sc}')
return Clock(self.seconds - sc)

def __rsub__(self, other):


print('\t __rsub__')
print(f'\t self = {self}')
print(f'\t other = {other}')
return self - other

def __isub__(self, other):


print('\t __isub__')
print(f'\t self = {self}')
print(f'\t other = {other}')
if not isinstance(other, (int, Clock)):
raise ArithmeticError("������ ������� ������ ���� ����� int ���
�������� Clock")
sc = other if isinstance(other, int) else other.seconds
print(f'\t sc = {sc}')
self.seconds -= sc
return self

# x * y ---------------------------------------
def __mul__(self, other):
print('\t __mul__')
print(f'\t self = {self}')
print(f'\t other = {other}')
if not isinstance(other, (int, Clock)):
raise ArithmeticError("������ ������� ������ ���� ����� int ���
�������� Clock")
sc = other if isinstance(other, int) else other.seconds
print(f'\t sc = {sc}')
return Clock(self.seconds * sc)

def __rmul__(self, other):


print('__rmul__')
print(f'\t self = {self}')
print(f'\t other = {other}')
return self * other

def __imul__(self, other):


print('\t __imul__')
print(f'\t self = {self}')
print(f'\t other = {other}')
if not isinstance(other, (int, Clock)):
raise ArithmeticError("������ ������� ������ ���� ����� int ���
�������� Clock")
sc = other if isinstance(other, int) else other.seconds
print(f'\t sc = {sc}')
self.seconds *= sc
return self

# x / y ---------------------------------------
def __truediv__(self, other):
print('\t __truediv__')
print(f'\t self = {self}')
print(f'\t other = {other}')
if not isinstance(other, (int, Clock)):
raise ArithmeticError("������ ������� ������ ���� ����� int ���
�������� Clock")
sc = other if isinstance(other, int) else other.seconds
print(f'\t sc = {sc}')
return Clock(self.seconds // sc)

# ��� ������ ������� (�� // )


# �.�. ����� ��������� ����� __truediv__
# � ������ self / other
def __rtruediv__(self, other):
print('__rtruediv__')
print(f'\t self = {self}')
print(f'\t other = {other}')
return self / other

def __itruediv__(self, other):


print('\t __itruediv__')
print(f'\t self = {self}')
print(f'\t other = {other}')
if not isinstance(other, (int, Clock)):
raise ArithmeticError("������ ������� ������ ���� ����� int ���
�������� Clock")
sc = other if isinstance(other, int) else other.seconds
print(f'\t sc = {sc}')
self.seconds //= sc
return self
c = Clock(100)
print(f'c = {c}')
print(c.__dict__)
print()
z = Clock(100000)
print(f'z = {c}')
print(z.__dict__)
print()
z1 = Clock(200)
print(f'z1 = {z1}')
print(z1.__dict__)
print('----------------- ')
print()
print()
print('----------------- z = z / 100')
print(f'z = {z}')
print(z.__dict__)
print('���������: ')
z = z / 100
print(f'z = {z}')
print(z.__dict__)
print('----------------- z = z / 100')
print('----------------- z = z * 100')
print(f'z = {z}')
print(z.__dict__)
print('���������: ')
z = z * 100
print(f'z = {z}')
print(z.__dict__)
print('----------------- z = z * 100')
print()
print()
print('----------------- z = 100 / z')
print(f'z = {z}')
print(z.__dict__)
print('���������: ')
z = 100 / z
print(f'z = {z}')
print(z.__dict__)
print('----------------- z = 100 / z')
print('----------------- z = 100 * z')
print(f'z = {z}')
print(z.__dict__)
print('���������: ')
z = 100 * z
print(f'z = {z}')
print(z.__dict__)
print('----------------- z = 100 * z')
print()
print()
print('----------------- z /= 100')
print(f'z = {z}')
print(z.__dict__)
print('���������: ')
z /= 100
print(f'z = {z}')
print(z.__dict__)
print('----------------- z /= 100')
print('----------------- z *= 100')
print(f'z = {z}')
print(z.__dict__)
print('���������: ')
z *= 100
print(f'z = {z}')
print(z.__dict__)
print('----------------- z *= 100')
print()
print()
print('----------------- z = z / z1')
print(f'z = {z}')
print(z.__dict__)
print(f'z1 = {z1}')
print(z1.__dict__)
z = z / z1
print('���������: ')
print(f'z = {z}')
print(z.__dict__)
print('----------------- z = z / z1')
print('----------------- z = z * z1')
print(f'z = {z}')
print(z.__dict__)
print(f'z1 = {z1}')
print(z1.__dict__)
z = z * z1
print('���������: ')
print(f'z = {z}')
print(z.__dict__)
print('----------------- z = z * z1')
print()
print()
print('----------------- z /= z1')
print(f'z = {z}')
print(z.__dict__)
print(f'z1 = {z1}')
print(z1.__dict__)
z /= z1
print('���������: ')
print(f'z = {z}')
print(z.__dict__)
print('----------------- z /= z1')
print('----------------- z *= z1')
print(f'z = {z}')
print(z.__dict__)
print(f'z1 = {z1}')
print(z1.__dict__)
z *= z1
print('���������: ')
print(f'z = {z}')
print(z.__dict__)
print('----------------- z *= z1')
print()
print()
print()
print()
print('----------------- z = z - 100')
print(f'z = {z}')
print(z.__dict__)
print('���������: ')
z = z - 100
print(f'z = {z}')
print(z.__dict__)
print('----------------- z = z - 100')
print('----------------- z = z + 100')
print(f'z = {z}')
print(z.__dict__)
print('���������: ')
z = z + 100
print(f'z = {z}')
print(z.__dict__)
print('----------------- z = z + 100')
print()
print()
print('----------------- z = 100 - z')
print(f'z = {z}')
print(z.__dict__)
print('���������: ')
z = 100 - z
print(f'z = {z}')
print(z.__dict__)
print('----------------- z = 100 - z')
print('----------------- z = 100 + z')
print(f'z = {z}')
print(z.__dict__)
print('���������: ')
z = 100 + z
print(f'z = {z}')
print(z.__dict__)
print('----------------- z = 100 + z')
print()
print()
print('----------------- z -= 100')
print(f'z = {z}')
print(z.__dict__)
print('���������: ')
z -= 100
print(f'z = {z}')
print(z.__dict__)
print('----------------- z -= 100')
print('----------------- z += 100')
print(f'z = {z}')
print(z.__dict__)
print('���������: ')
z += 100
print(f'z = {z}')
print(z.__dict__)
print('----------------- z += 100')
print()
print()
print('----------------- z = z - z1')
print(f'z = {z}')
print(z.__dict__)
print(f'z1 = {z1}')
print(z1.__dict__)
z = z - z1
print('���������: ')
print(f'z = {z}')
print(z.__dict__)
print('----------------- z = z - z1')
print('----------------- z = z + z1')
print(f'z = {z}')
print(z.__dict__)
print(f'z1 = {z1}')
print(z1.__dict__)
z = z + z1
print('���������: ')
print(f'z = {z}')
print(z.__dict__)
print('----------------- z = z + z1')
print()
print()
print('----------------- z -= z1')
print(f'z = {z}')
print(z.__dict__)
print(f'z1 = {z1}')
print(z1.__dict__)
z -= z1
print('���������: ')
print(f'z = {z}')
print(z.__dict__)
print('----------------- z -= z1')
print('----------------- z += z1')
print(f'z = {z}')
print(z.__dict__)
print(f'z1 = {z1}')
print(z1.__dict__)
z += z1
print('���������: ')
print(f'z = {z}')
print(z.__dict__)
print('----------------- z += z1')

print()
print()
print('----------------- z = z - z1 - c')
print(f'z = {z}')
print(z.__dict__)
print(f'z1 = {z1}')
print(z1.__dict__)
print(f'c = {c}')
print(c.__dict__)
z = z - z1 - c
print('���������: ')
print(f'z = {z}')
print(z.__dict__)
print('----------------- z = z - z1 - c')
print('----------------- z = z + z1 + c')
print(f'z = {z}')
print(z.__dict__)
print(f'z1 = {z1}')
print(z1.__dict__)
print(f'c = {c}')
print(c.__dict__)
z = z + z1 + c
print('���������: ')
print(f'z = {z}')
print(z.__dict__)
print('----------------- z = z + z1 + c')
print()
print()

������������� ��� �����:


(�� �������� ����������� ������ : ������� �������� � ����� ������)

class Clock:
__DAY = 86400 # ����� ������ � ����� ���

def __init__(self, seconds: int):


if not isinstance(seconds, int):
raise TypeError('������� ������ ���� ������')
self.seconds = seconds % self.__DAY

# ����� ��������� ��������� ��� ��������� � print() / print(c)


def __repr__(self):
s = self.seconds % 60 # �������
m = (self.seconds // 60) % 60 # ������
h = (self.seconds // 3600) % 24 # ����
return f'{self.__form(h)}:{self.__form(m)}:{self.__form(h)}'

@classmethod
def __form(cls,x):
return str(x).rjust(2, '0')

@classmethod
def __check(cls,other):
# print(f'cls {cls}') # cls <class '__main__.Clock'>
# print(f'Clock {Clock}') # Clock <class '__main__.Clock'>
# print(f'__class__ {__class__}') # __class__ <class '__main__.Clock'>
# if not isinstance(other, (int, Clock)):
if not isinstance(other, (int, cls)):
raise ArithmeticError("������ ������� ������ ���� ����� int ���
�������� Clock")
sc = other if isinstance(other, int) else other.seconds
return sc

# x + y ---------------------------------------
# ���� ��������, ��� ��� ���������� _add_
# ����� ��������� � ������ ������ ����� self.__class__ ,
# return self.__class__(self.seconds + sc)
# � �� ������ ����� �������� Clock
# return Clock(self.seconds + sc).
# ������ ��� ������� ������� ���� �������� ������,
# � ������ ������ ������ ������� ��� ���� �� ������.
def __add__(self, other):
print('__add__')
sc = self.__check(other)
print(f'sc = {sc}')
return self.__class__(self.seconds + sc)

def __radd__(self, other):


print('__radd__')
return self + other

def __iadd__(self, other):


print('__iadd__')
sc = self.__check(other)
print(f'sc = {sc}')
self.seconds += sc
return self

# x - y ---------------------------------------
def __sub__(self, other):
print('__sub__')
sc = self.__check(other)
print(f'sc = {sc}')
return self.__class__(self.seconds - sc)

def __rsub__(self, other):


print('__rsub__')
return self - other

def __isub__(self, other):


print('__isub__')
sc = self.__check(other)
print(f'sc = {sc}')
self.seconds -= sc
return self

# x * y ---------------------------------------
def __mul__(self, other):
print('__mul__')
sc = self.__check(other)
print(f'sc = {sc}')
return self.__class__(self.seconds * sc)

def __rmul__(self, other):


print('__rmul__')
return self * other

def __imul__(self, other):


print('__imul__')
sc = self.__check(other)
print(f'sc = {sc}')
self.seconds *= sc
return self
# x / y ---------------------------------------
def __truediv__(self, other):
print('__truediv__')
sc = self.__check(other)
print(f'sc = {sc}')
return self.__class__(self.seconds // sc)

# ��� ������ ������� (�� // )


# �.�. ����� ��������� ����� __truediv__
# � ������ self / other
def __rtruediv__(self, other):
print('__rtruediv__')
return self / other

def __itruediv__(self, other):


print('__irtruediv__')
sc = self.__check(other)
print(f'sc = {sc}')
self.seconds //= sc
return self

������������� ��� �����:


(�� �������� ����������� ������ : ������������ ���������� ������)
# # ��������� � ��� ������, ������ �������� �������� ����� ������
# # �� ��������� � ���������������� ��� ����������������� �������� � ����.

class Clock:
__DAY = 86400 # ����� ������ � ����� ���

def __init__(self, seconds: int):


if not isinstance(seconds, int):
raise TypeError('������� ������ ���� ������')
self.seconds = seconds % self.__DAY

# ����� ��������� ��������� ��� ��������� � print() / print(c)


def __repr__(self):
s = self.seconds % 60 # �������
m = (self.seconds // 60) % 60 # ������
h = (self.seconds // 3600) % 24 # ����
return f'{self.__form(h)}:{self.__form(m)}:{self.__form(h)}'

@classmethod
def __form(cls,x):
return str(x).rjust(2, '0')

@classmethod
def __check(cls,other):
# print(f'cls {cls}') # cls <class '__main__.Clock'>
# print(f'Clock {Clock}') # Clock <class '__main__.Clock'>
# print(f'__class__ {__class__}') # __class__ <class '__main__.Clock'>
# if not isinstance(other, (int, Clock)):
if not isinstance(other, (int, cls)):
raise ArithmeticError("������ ������� ������ ���� ����� int ���
�������� Clock")
sc = other if isinstance(other, int) else other.seconds
return sc

def decorF(func):
def wrapper(self, other):
sc = self.__check(other)
res = func(self, sc)
return res
return wrapper

# x + y ---------------------------------------
@decorF
def __add__(self, other):
print('\t __add__')
print(f'\t self = {self}')
print(f'\t other = {other}')
self.seconds += other
return self

def __radd__(self, other):


print('\t __radd__')
print(f'\t self = {self}')
print(f'\t other = {other}')
return self + other

@decorF
def __iadd__(self, other):
print('\t __iadd__')
print(f'\t self = {self}')
print(f'\t other = {other}')
self.seconds += other
return self

# x - y ---------------------------------------
@decorF
def __sub__(self, other):
print('\t __sub__')
print(f'\t self = {self}')
print(f'\t other = {other}')
self.seconds -= other
return self

def __rsub__(self, other):


print('\t __rsub__')
print(f'\t self = {self}')
print(f'\t other = {other}')
return self - other

@decorF
def __isub__(self, other):
print('\t __isub__')
print(f'\t self = {self}')
print(f'\t other = {other}')
self.seconds -= other
return self

# x * y ---------------------------------------
@decorF
def __mul__(self, other):
print('\t __mul__')
print(f'\t self = {self}')
print(f'\t other = {other}')
self.seconds *= other
return self

def __rmul__(self, other):


print('__rmul__')
return self * other

@decorF
def __imul__(self, other):
print('\t __imul__')
print(f'\t self = {self}')
print(f'\t other = {other}')
self.seconds *= other
return self

# x / y ---------------------------------------
@decorF
def __truediv__(self, other):
print('\t __truediv__')
print(f'\t self = {self}')
print(f'\t other = {other}')
self.seconds //= other
return self

# ��� ������ ������� (�� // )


# �.�. ����� ��������� ����� __truediv__
# � ������ self / other
def __rtruediv__(self, other):
print('__rtruediv__')
return self / other

@decorF
def __itruediv__(self, other):
print('\t __itruediv__')
print(f'\t self = {self}')
print(f'\t other = {other}')
self.seconds //= other
return self
# ==========================================================#
==========================================================
# #
=======================================
# ���������� ������ ���������
#
# __eq__() � �� ��������� ==
# __ne__() � �� ����������� !=
# __lt__() � �� ��������� ������ <
# __le__() � �� ��������� ������ ��� ����� <=
# __gt__() � �� ��������� ������ >
# __ge__() � �� ��������� ������ ��� ����� >=
#

class Clock:
__DAY = 86400 # ����� ������ � ����� ���

def __init__(self, seconds: int):


if not isinstance(seconds, int):
raise TypeError('������� ������ ���� ������')
self.seconds = seconds % self.__DAY

# ����� ��������� ��������� ��� ��������� � print() / print(c)


def __repr__(self):
s = self.seconds % 60 # �������
m = (self.seconds // 60) % 60 # ������
h = (self.seconds // 3600) % 24 # ����
return f'{self.__form(h)}:{self.__form(m)}:{self.__form(h)}'

@classmethod
def __form(cls,x):
return str(x).rjust(2, '0')

@classmethod
def __check(cls,other):
if not isinstance(other, (int, cls)):
raise ArithmeticError("������ ������� ������ ���� ����� int ���
�������� Clock")
sc = other if isinstance(other, int) else other.seconds
return sc

def decorF(func):
def wrapper(self, other):
sc = self.__check(other)
res = func(self, sc)
return res
return wrapper

# ���������� ������ �������������� ��������


# x + y ---------------------------------------
@decorF
def __add__(self, other):
self.seconds += other
return self

def __radd__(self, other):


return self + other

@decorF
def __iadd__(self, other):
self.seconds += other
return self

# x - y ---------------------------------------
@decorF
def __sub__(self, other):
self.seconds -= other
return self

def __rsub__(self, other):


return self - other

@decorF
def __isub__(self, other):
self.seconds -= other
return self

# x * y ---------------------------------------
@decorF
def __mul__(self, other):
self.seconds *= other
return self

def __rmul__(self, other):


return self * other

@decorF
def __imul__(self, other):
self.seconds *= other
return self

# x / y ---------------------------------------
@decorF
def __truediv__(self, other):
self.seconds //= other
return self

def __rtruediv__(self, other):


return self / other

@decorF
def __itruediv__(self, other):
self.seconds //= other
return self

# ���������� ������ ���������


# x == y ---------------------------------------
@decorF
def __eq__(self, other):
return self.seconds == other

# x != y ---------------------------------------
@decorF
def __ne__(self, other):
return self.seconds != other

# x < y ---------------------------------------
@decorF
def __lt__(self, other):
return self.seconds < other
# x > y ---------------------------------------
@decorF
def __gt__(self, other):
return self.seconds > other

# x <= y ---------------------------------------
@decorF
def __le__(self, other):
return self.seconds <= other

# x >= y ---------------------------------------
@decorF
def __ge__(self, other):
return self.seconds >= other

# ���������� �� ������ ���������� ������ ���� ����� �������� �� ���������,


# ��������:

c1 = Clock(1000)
c2 = Clock(1000)
print('----------------- x == y')
# ���� ��������� �������� �� ==
# print(c1 == c2) # False ��� ����������� ������ __eq__() � ������
# ��� ���������� ������, ��� ���������� id (������ � �����)
# ��� ������ � ��������� �������������� False
# ��� ����� ���������� �������, �� ����� ����������
# �������������� ���������� ����� __eq__() � ������
# ���������� � ������� ��� � �� ���������� �������������� �������
# �������� if not isinstance(other, (int, cls)):
# ����������� other � ������ ������ __check(cls,other)
# ���� �� int �� ��� �������� ������ other.seconds
# sc = other if isinstance(other, int) else other.seconds
# ������ � ������� __eq__() ����� ��������� decorF(func):
# ������ ��������
print(c1 == c2) # True
print(c1 == 1000) # True
print()
print()
print('----------------- x != y')
# ���� ��������� �������� �� !=
# print(c1 != c2) # False ��� ����������� ������ __ne__() � ������
# print(c1 != 1000) # False ��� ����������� ������ __ne__() � ������
# �� ��� ��������, ��� ����������� ������ �� != ��� � ������
# ��� ���������� ������, ��� �������� Python
# ����� ����� c1 != c2 � ���������� ����� �� != �� ���������� � ������
# �� ������� ��������� not(c1 == c2), ���������� �����
# � ���������� ����� �� == ���������� � ������
# �������� Python ���������� ��������� � ����������� ���������
# ��-�������� ��� �������� ���
# �������������� ���������� ����� __ne__() � ������
# ������ ��������� ����������� ���� �����
print(c1 != c2) # False
print(c1 != 1000) # False
print()
print()
print('----------------- x < y')
# ���� ��������� �������� �� <
# print(c1 < c2) # False ��� ����������� ������ __lt__() � ������
# print(c1 < 1000) # False ��� ����������� ������ __lt__() � ������
# ��������� �����
# TypeError: '<' not supported between instances of 'Clock' and 'Clock'
# �������������� ���������� ����� __lt__() � ������
# ������ ��������
print(c1 < c2) # False
print(c1 < 1000) # False
print()
print()
print('----------------- x > y')
# ���� ��������� �������� �� >
# print(c1 > c2) # False ��� ����������� ������ __gt__() � ������
# print(c1 > 1000) # False ��� ����������� ������ __gt__() � ������
# �� ��� ��������, ��� ����������� ������ �� > ��� � ������
# ��� ���������� ������, ��� �������� Python
# ����� ����� c1 > c2 � ���������� ����� �� > �� ���������� � ������
# �� ������� ��������� not(c1 < c2), ���������� �����
# � ���������� ����� �� < ���������� � ������
# �������� Python ���������� ��������� � ����������� ���������
# ��-�������� ��� �������� ���
# �������������� ���������� ����� __gt__() � ������
# ������ ��������� ����������� ���� �����
print(c1 > c2) # False
print(c1 > 1000)
print()
print()
print('----------------- x <= y')
# ���������� � ������ ��������� ����������
print(c1 <= c2) # True
print(c1 <= 1000) # True
print()
print()
print('----------------- x >= y')
# ���������� � ������ ��������� ����������
print(c1 >= c2) # True
print(c1 >= 1000) # True
# ��-���� ���������� � ������ ����������� ��������� ������
# __eq__() � �� ��������� ==
# __lt__() � �� ��������� ������ <
# __le__() � �� ��������� ������ ��� ����� <=
# ��������� �������� � ��������� ������
#
#
# #
=======================================
# ���������� ������ __eq__ � __hash__
#
# __eq__() � �� ��������� ==
# __hash__() � �� ��������� ���� ��������
#
# ��� ������ ������ ����� �����

class Point:

def __init__(self, x, y):


self.x = x
self.y = y
def __eq__(self, other):
return isinstance(other, Point) and \
self.x == other.x and self.y == other.y

def __hash__(self):
return hash((self.x, self.y))

# ���� �������� � Python ��� ������


# � ������� ������� ���� ���� id
# print(type(123)) # <class 'int'> �����
# print(isinstance(123, object)) # True
# print(id(123)) # 1556328272
#
# print(type((1,2,3))) # <class 'tuple'> ������
# print(isinstance((1,2,3), object)) # True
# print(id((12,3))) # 46050056
#
# print(type([1,2,3])) # <class 'list'> ������
# print(isinstance([1,2,3], object)) # True
# print(id([1,2,3])) # 13352776
#
# print(type({1,2,3})) # <class 'set'> ���������
# print(isinstance({1,23}, object)) # True
# print(id({1,2,3})) # 46452776
#
# print(type('Python')) # <class 'str'> ������
# print(isinstance('Python', object)) # True
# print(id('Python')) # 7813056
# print()
#
# � Python ������ ���������� ������ ��������� ���� �� ��������,
# ������ ��������� �� ������������� ���������
# ������������� ������� �� ����������� ��������.
# ������ [1,2,3] ��������� ������ hash() �� ��������
# ������� ������ �unhashable type� - �� ���������� ������.
# ������ hash() - ��� ������������� ������,
# ������ �������� �������� � ������ ������������ ��������������
# � ���������� �������� ��������
# ������� �� ��������� ������� �������� ������ �����.
# ���������� ����� __hash__() ��� �� �� �����
#
#
# print(hash(123)) # 123
# print(hash("Python")) # 926916373
# print(hash((1, 2, 3))) # -2022708474
# ������, �� ������ �������� �� ������ ������ ������ ��������� ������ ����
# print(hash(123)) # 123
# print(hash("Python")) # 926916373
# print(hash((1, 2, 3))) # -2022708474
# � ��� �������� ����������� ������ �����:
# ������ ���� �� ���������� ��������� ��������.
# ���, ��� � ��������� ���������: ������� � ��� ����, �� �� ����� ���� �������.
# � ������ ��� �� �� �����.
# ������, ���� ���� �� �����, �� � ������� ����� �� �����.
# �������� �������� �������� �� ����:
# ���� ������� a == b (�����), �� ����� � �� ���.
# ���� ����� ����: hash(a) == hash(b), �� ������� ����� ���� �����,�� ����� ���� �
�� �����.
# ���� ���� �� �����: hash(a) != hash(b), �� ������� ����� �� �����.
#
# �� ����� ��� ��� ����?
# � ���������������� ��������� ������� � Python,
# ��������, ������� ��������� ���� � �������� ����� �����.
# ��������, ����� �� � ������ ��������� ���,
# �� �� ������ ��������� � ������������ ���� ������:
# d = {}
# d[5] = 5
# d["python"] = "python"
# d[(1, 2, 3)] = [1, 2, 3]
# � ����������������, ��� ����������,
# ����� ����� ���� ��������� ��� �������� � ���� ������� � ����:
# (��� ����, ���)
# ���� � ���, ��� ������������� ����� ������ � ������� ����� �� ����,
# ��� ��� ���������� ������� �������� ������ ������� ������� ����.
# �, �����, �� ������ ����� (���� ����� ���� ����������),
# ��������� ������ � ��������� � ���� �������.
# ����� ������ ����������� ������� ����� ������� � �������.
#
# �������� ��� �� ���������� ������
# p1 = Point(1, 2)
# p2 = Point(1, 2)
# print(hash(p1), hash(p2), sep='\n')
# ���������:
# 2890268
# 2906612
# ���������� ����� __hash__() ��� �� �� �����
# print(p1.__hash__(), p2.__hash__(), sep='\n')
# ���������:
# 2890268
# 2906612
#
# print(id(p1), id(p2), sep='\n')
# 46051776
# 46309328
# print(p1 == p2) # False ��� ����������� ������ __eq__()
# �������� ��������, ������� �� ��,
# ��� ���������� ����� p1 � p2 �����, �� ���� ������.
# �� ����, � ����� ����� ������� hash() � ��� ��� ������ �������.
# ��������� ���������� �� id ��������
# ��������������, �� ������ �������� ����� ��������� � ������ ����.
# �� ���������� ����� ��������� � �� ����� ���������� ������ ��
# �� ����� ������������� � ������ ���������� �����
# __eq__() � �� ��������� ==
# ������� ��������:
# ����� �� other ������� Point (������ ������ Point)
# ���� �� �� ���������� ���������� x y �������
# ���� �� ��������� ����� ���� �� ���� ��� ��������
# �� ������ �� �����
# print(p1 == p2) # True
#
# print(id(p1), id(p2), sep='\n')
# 46051776
# 46309328
# id ������, �� ������ ���������� �� �����������
# ������ � ��� ������� � ����������� ������������ ����� �������� �������.
# �� ��� ������� ���� ��� � �������
# print(p1.__hash__(), p2.__hash__(), sep='\n') ��� __eq__()
# ��������� ������
# TypeError: 'NoneType' object is not callable
# �� ����, ���� ������� ����� �� �����������
# ��� ������ ���������� ��������������� ��������� ==,
# �� ��������� �������� ��������� ���� �� ����� �������� ��������� ��������.
# �������, ��� ����� ����� ��������� ���� ������ ��������� ���� ��������
# ����� ���������� ����� __hash__()
# ������� �������� �� ������� (self.x, self.y)
# ������ �������� � ������������ ����, ������� �� ����
# ����� ��������� ���������� ������ hash().
# �� ����, �� ��������� ���������� ���� ������� ������ Point
# �� ���������� ���� �� ��������� �����.
# print(hash(p1), hash(p2), sep='\n')
# print(p1.__hash__(), p2.__hash__(), sep='\n')
# ������ �����, ��� ������� ����� � �� ���� ����� �����.
# ��� ��� � ����� ��������?
# ��������, ���� ���� ������ �������:
# d = {}
# �, �����, ������������ ������ ����� ������� p1 � p2:
# d[p1] = 1
# d[p2] = 2
# print(d) # {<__main__.Point object at 0x02BEB1C0>: 2}
# �� ��� ����� ������������� ��� ���� � ��� �� ���,
# ��� ��� ������� ����� � �� ���� ���� �����.
#
# ������ ������� ������ � �� ���� ���� ������.
b1 = Point(2,4)
b2 = Point(4,6)
b3 = Point(6,8)
d = {}
d[b1] = 1
d[b2] = 2
#
#
# #
=======================================
# ���������� ����� __bool__
#
# __len__() � ��������� �������� bool(),
# ���� �� ��������� ���������� ����� __bool__();
#
# __bool__() � ��������� � ������������ ������ �������� bool(),
# ���� ���� �������� ���������� ����� __len__();
#
# ��� ������ ������ ����� �����

class Point:

def __init__(self, x, y):


self.x = x
self.y = y

def __len__(self):
print('__len__')
return abs(self.x - self.y)
def __bool__(self):
print('__bool__')
return self.x != 0 or self.y != 0

# ���� �������� � Python ��� ������


# � ������ ������ ���� ������(True) ���� ����(False)

# print(type(123)) # <class 'int'> �����


# print(bool(123)) # True
# print(type(0)) # <class 'int'> �����
# print(bool(0)) # False
# ���� ����� (������������� ��� �������������) ����� True
# ����� 0 (����) ���� False
#
# ��� ����� ��� � ��� ��������� �������:
# �� ������ ������ ���� True
# ������ ������ ����� False
#
#
#
# ������
# print(type((1,2,3))) # <class 'tuple'> ������
# print(bool((1,2,3))) # True
# print(type(())) # <class 'tuple'> ������
# print(bool(())) # False
#
# ������
# print(type([1,2,3])) # <class 'list'> ������
# print(bool([1,2,3])) # True
# print(type([])) # <class 'list'> ������
# print(bool([])) # False
#
# ���������
# print(type({1,2,3})) # <class 'set'> ���������
# print(bool({1,23})) # True
# print(type({})) # <class 'set'> ���������
# print(bool({})) # False
#
# ������
# print(type('Python')) # <class 'str'> ������
# print(bool('Python')) # True
# print(type('')) # <class 'str'> ������
# print(bool('')) # False
#
# � Python ����������� ���������:
# ���� �������� ������ ����� True
# � �� ����� ����� �������
# p = Point(1,4)
# print(type(p)) # <class '__main__.Point'> �������� ������ Point
# print(bool(p)) # True
# p1 = Point(0,0)
# print(type(p1)) # <class '__main__.Point'> �������� ������ Point
# print(bool(p1)) # True
# ����� ��������� ����������, ����� �� ����������� __len__() � __bool__()
#
# ���� ����� �������� ���� ������, �� ���������� ���������� ��
# ������ ���������:
# ������� ����� ������ __bool__()
# ���� ��� ��� ����� ������ __len__()
# ���� ��� ��� ���������� ������ True
#
# ��������� ���������� ����� __len__()
# ����� ����������� ����� ����-��, � � ��� �����
# ����� ����� ���������� ������ x-y (�.�. ���������)
#
# p = Point(1,4)
# print(type(p)) # <class '__main__.Point'> �������� ������ Point
# print(bool(p)) # True
# p1 = Point(0,0)
# print(type(p1)) # <class '__main__.Point'> �������� ������ Point
# print(bool(p1)) # False
#
# ��������� ���������� ����� __bool__()
# ����� ����������� True / False
# ���� ��������� ������� ����-�� �������(�� ����������) = ������
# ����� ����� ���������� False ���� ���������� ����� (0,0)
# � ��������� ������ ���������� True
#
p = Point(1,4)
print(type(p)) # <class '__main__.Point'> �������� ������ Point
print(bool(p)) # True
p1 = Point(0,0)
print(type(p1)) # <class '__main__.Point'> �������� ������ Point
print(bool(p1)) # False
p2 = Point(0,5)
print(type(p2)) # <class '__main__.Point'> �������� ������ Point
print(bool(p2)) # True
p3 = Point(-5,0)
print(type(p3)) # <class '__main__.Point'> �������� ������ Point
print(bool(p3)) # True
print( )
#
# ���� ������ bool() �������� ����
# ��� ������ ����� (� �������� ����������) ��������� ���������� �����
# ���� ���������� ����� __bool__() ���� ����, ���� ���������� ����� __len__()
#
if p:
print(f'������ {p} ��� True')
else:
print(f'������ {p} ��� False')
#
# ���������:
# ������ <__main__.Point object at 0x02C4B1C0> ��� True
#
if p1:
print(f'������ {p1} ��� True')
else:
print(f'������ {p1} ��� False')
#
# ���������:
# ������ <__main__.Point object at 0x02C88070> ��� False
#
#
# #
=======================================
# ���������� ������
#
# __getitem__(self, item) � ��������� ������� �� ���� item;
# __setitem__(self, key, value) � ������ ������� value �� ���� key;
# __delitem__(self, key) � �������� �������� �� ���� key.
#
# ������� ����� �� ������������ ���������:
# name - �� ��������
# marks - ������ ��� ������
class Student:

def __init__(self, name, marks):


self.name = name
self.marks = list(marks)

def __getitem__(self, item):


if isinstance(item, int) and \
0 <= item < len(self.marks):
return self.marks[item]
else:
raise IndexError(f"������ {item} ������ ���� ����� ������ � ���������
[0:{len(self.marks)-1}] ")

def __setitem__(self, key, value):


if not isinstance(key, int) or key < 0:
raise TypeError(f"������ {key} ������ ���� ������ ���������������
�����")
if key >= len(self.marks):
off = key + 1 - len(self.marks)
self.marks.extend([None]*off)
self.marks[key] = value

def __delitem__(self, key):


if isinstance(key, int) and \
0 <= key < len(self.marks):
del self.marks[key]
else:
raise IndexError(f"������ {item} ������ ���� ����� ������ � ���������
[0:{len(self.marks)-1}] ")

# ������� �������� ������


s1 = Student('Djon', [5,5,4,2,5])
# ��������� � ��������� � ������� ������� �� �������
print(s1.marks[2]) # 4
# � �������� ������ (���������) ����� ��������� �� �������
# � ������� ��� �� ��������, ������ �� ����������� ������� �������������
# print(s1[2]) # TypeError: 'Student' object is not subscriptable
# �� �������� ����� ������ ����� ����������
# ���������� ����� __getitem__(self, item)
# self - ��� ������ �� �������� ������
# item - ��� ������
# �� ����������� ������
# print(s1.marks[20]) # IndexError: list index out of range
# � ������ ��������� �������� ������� �� int � �� �������� [ 0; len(marks) ]
print(s1[2]) # 4
# ������ ��� �� �� ����������� ������� �������� ������� ��������
# s1[2] = 10 # TypeError: 'Student' object does not support item assignment
# �� �������� ����� ������ ����� ����������
# ���������� ����� __setitem__(self, key, value)
# self - ��� ������ �� �������� ������
# key - ��� ������
# value - ��� ��� ��������
# �� ����������� ������
# � ������ ��������� �������� ������� �� int � �� �������������
# ������� ������� ����� ���� � �������������� (������� �����)
# �� � �������� �������� ������� ��������� ����� �������
# ���� ������ ����� �������� �� ��������
# s1[10] = 5 # NameError: name 'value' is not defined
# � ������ ��� �� ��������� �������� ������� �� �������� [ 0; len(marks) ]
# � ���� �� ������ if key >= len(self, marks):
# �� �������� ������ �� ������� ��������� off = key + 1 - len(self.marks)
# � ������ ������ �������
# extend(items): �������� ����� ��������� items � ����� ������
s1[10] = 5
print(s1.marks) # [5, 5, 4, 2, 5, None, None, None, None, None, 5]
# ������ ��� �� �� ����������� ������� �������� ��������
# del s1[10] # AttributeError: __delitem__
# ���������� ����� __delitem__(self, key)
# self - ��� ������ �� �������� ������
# key - ��� ������
# �� ����������� ������
# � ������ ��������� �������� ������� �� int � �� �������� [ 0; len(marks) ]
del s1[10]
print(s1.marks) # [5, 5, 4, 2, 5, None, None, None, None, None]
#
#
# �� ������:
#
# ������� ����� �� ����� �����:
class Vector:

def __init__(self, *args):


self.values = list(args)

# ������������� __repr__ �� ������


def __repr__(self):
return str(self.values)

def __getitem__(self, item):


if 0 <= len(self.values):
return self.values[item]
else:
raise IndexError('������ �� ��������� ���������')

def __setitem__(self, key, value):


if 0 <= key <= len(self.values):
self.values[key] = value
elif key > len(self.values):
diff = (key - len(self.values)) + 1
self.values.extend([None]*diff)
self.values[key] = value
else:
raise IndexError('������ �� ��������� ���������')

def __delitem__(self, key):


if 0 <= len(self.values):
del self.values[key]
else:
raise IndexError('������ �� ��������� ���������')

v = Vector(1,2,3,4,5)
print(v) # [1, 2, 3, 4, 5]
print(v[3]) # 4
v[3] = 7
print(v) # [1, 2, 3, 7, 5]
v[9] = 57
print(v) # [1, 2, 3, 7, 5, None, None, None, None, 57]
v[7] = 77
print(v) # [1, 2, 3, 7, 5, None, None, 77, None, 57]
#
#
# #
=======================================
# ���������� ������
#
# __iter__(self) � ��������� ��������� �� �������� �������;
# __next__(self) � ������� � ��������� ������� � ��� ����������.
#
# ���� ������ - ��� ����������� ������
# ������� �� ���� ����� ������� ��������.
# lst = [8,5,3,1,7]
# print(lst)
# �������� - ��� ����� ��������� �� ��������
# ��������� ����� ������������ �������.
# it = iter(lst)
# � ������ ����������������� ������ ������� next()
# ������ ��������� �� ������������ �������
# print(next(it)) # 8
# print(next(it)) # 5
# print(next(it)) # 3
# print(next(it)) # 1
# print(next(it)) # 7
# ���������� ������ �. next() = ����� ������ len(lst)
# ��� ������
# print(next(it)) # StopIteration
# ��� ������������� ������ �������� ����� ������������ �������
# (������, ������, �������, ������ ...)
# ������ range(start,stop,step) - ������������� ������������������
# ���� ������ ����������� ��������
# � ������� ���� ������������������ ����� ��������� � ������ ���������
# print( list(range(5)) ) # [0, 1, 2, 3, 4]
# �.list() ������ ��� ������������ ��������� ��� �������� �.range()
# ��� ����� ������� � ������
# �� ����� ������� �������� �� �.range()
# a = iter(range(5))
# ����� � ������ �.next() ��������� ��������
# print( next(a) ) # 0
# print( next(a) ) # 1
# print( next(a) ) # 2
# print( next(a) ) # 3
# print( next(a) ) # 4
#
# �� ������ ������� �������� ����������� ������
# �������� ���������� ������ __iter__(self) � __next__(self)
# ������� ��� �� ������� ������ FRange
# ������� ����� ���������� ������������ ������������������
# ������������ �����
# ������� ����� :
class FRange:
def __init__(self, start=0.0, stop=0.0, step=1.0):
self.start = start
self.stop = stop
self.step = step

def __iter__(self):
self.value = self.start - self.step
return self

def __next__(self):
if self.value + self.step < self.stop:
self.value += self.step
return self.value
else:
raise StopIteration

# �������� �������������� ������������������


# ��������� �� ������� start
# ������������ ��������� stop (�� ������� ���)
# ��� � ����� step
# �� �������� �������� ������� �������� __next__(self)
# � ������� ��������:
# ���� ������� �������� self.value ��� ��� self.step
# ������ ��������� ������� self.stop
# ����� �������� �������� self.value �� �������� ���� self.step
# � ����� �������� return self.value
# ����� �������� raise StopIteration
# � __init__ ��������� �������� self.value = self.start - self.step
#
# ������� �������� ������
# fr = FRange(0, 2, 0.5)
# ������� __next__(self)
# print(fr.__next__())
# print(fr.__next__())
# print(fr.__next__())
# print(fr.__next__())
# ���������:
# 0.0
# 0.5
# 1.0
# 1.5
# �������� ����������� ������ __next__(self)
# ����� ��������� �� � ������ �.next()
# print(next(fr))
# print(next(fr))
# print(next(fr))
# print(next(fr))
# ��������� ��� ��
# �.�. �.next() �� ����, � ��� ������ �������� ���������� ����� __next__(self)
# � ���������� �������� ������� ����� ���������� �����
# � ��� ������ fr ��������� � ���� ���������
# ����������� ����� ����� ���������� � ������ for
# �� ����� ������ ������ ���� �����������
# � �� ������ ������ fr ������� �������� �����
# it = iter(fr) # TypeError: 'FRange' object is not iterable
# �.iter() �� ����� ���� ��������� � ������� fr
# ������ ��� � ������ �� �������� ���������� ����� __iter__(self)
# �������� ���
# ����� ���������� ��� ������ fr, �.�. �� ��� ������ ����������
# � ������ ������ � �������� ��������� ��������� ��� ������.
# iter() ���������� ��������, � �� ������
# (������ ������ = ��������, �� �� ������),
# �������������� next() ���������� ��������� ��������
# return self
# � ������������� value ����� � ���� ������, � �� � __init__
# self.value = self.start - self.step
# �.�. ������ ���, ����� ��������� �.iter()
# ��� �����, ������ ��� �������� ���������� ����� __iter__(self)
# � �������� self.value �������������� �� ������ �����. ������������������
# � ��� ����� ��������� iter(fr) ���������� ����� ������ ����� for
# for x in fr:
# print(x)
# ���������:
# 0.0
# 0.5
# 1.0
# 1.5
# ���� for ����� ������� �������� �������� itet()
# ����� � ������ ������� ������ �.next() ���������� ������� ������������������
# ��� ��� � ������ __iter__(self) � __next__(self)
# ���������� ��� ����� � ����������� ������
# � ����������� �������� �������� next() ��� ������ for.
# B __next__(self) ������� ��� ����� ������� �����������
# ��������� �������� ����� ������������ �������
# ��� ������� ����� ������� ������.
# ����� ������ ����� ���� ���:
# ����� ���� ������� �������
# ����� ���� ������� �������� � ����������� ������
# ��� ���-�� �� = ��� ������
#
#
# ������ ������ FRange2D �� ����������� ������ ��������:
# � �������������� �������� ���������� ������ FRange,
# ������� ����� ����������� ������ �������
# ( ����� �� ������ ������ )
# self.fr = FRange(start, stop, step)
# �������� rows � ����� �����
# self.rows = rows
# �����, �������� ��� ���������� ������ __iter__ � __next__

# ������� ����� :
class FRange2D:
def __init__(self, start=0.0, stop=0.0, step=1.0, rows=5):
self.fr = FRange(start, stop, step)
self.rows = rows

def __iter__(self):
self.value_row = 0
return self

def __next__(self):
if self.value_row < self.rows:
self.value_row += 1
return iter(self.fr)
else:
raise StopIteration
#
# ������ __iter__
# ��������� self.value � ��������� �������� ����
# self.value_row = 0
# ���������� ��� ������
# return self
#
# ������ __next__
# ���� �������� value ������ ������� rows = �� ������ ��� ������
# if self.valuet < self.rows:
# �� ����� ������������ ��� ������ (������ ��������)
# �� ����� �������� value �������� �� �������
# self.value += 1
# � ��������� �������� �� ������� fr
# return iter(self.fr)
# �.�. � ������ ������ ����� ������������(�����������) ������ fr
# � ����� �������� ��������
# raise StopIteration
#
# �������� ��������, ��� ����� __next__ ���������� �� ���������� ��������,
# � �������� �� ������ ������ FRange.
#
#
fr = FRange2D(0, 2, 0.5, 4)
for row in fr:
for x in row:
print(x, end=" ")
print()
# � ������ ������
# for row in fr:
# � �������� row �������� ������ ���
# �������� ������� return iter(self.fr)
# �� ��� ���������� __next__
# �� ������ ������
# for x in row:
# ���������� ���� �������� � �������� ���������� �������� �������
# ���������:
# 0.0 0.5 1.0 1.5
# 0.0 0.5 1.0 1.5
# 0.0 0.5 1.0 1.5
# 0.0 0.5 1.0 1.5
# ��� ����� ������� ������� ����������� ��������
# � �� ������ ������, ��� � �� ����
# ���������� ���������� ������ __iter__ � __next__.
#
#
# #
=======================================
# ������� : ����������� ����� ��� ������ �� ������
# ���������� ������
#
# __getitem__(self, item) � ��������� ������� �� ���� item;
# __setitem__(self, key, value) � ������ ������� value �� ���� key;
# __delitem__(self, key) � �������� �������� �� ���� key.
#

Вам также может понравиться