Jul 11, 2024
class ClassName:
# содержимое класса
Pointclass Point:
pass
color и circle:
class Point:
color = 'red'
circle = 2
color и circle.a = Point()
b = Point()
Point.color # 'red'
Point.circle # 2
Point.color = 'black' # изменили красный на черный
Point.__dict__
a = Point()
b = Point()
type(a) == Point # True
isinstance:
isinstance(a, Point) # True
color и circle доступны через объекты классов:
a.color # 'black'
b.circle # 2
a.color = 'green' # теперь у a свой атрибут color со значением 'green'
b.circle # 2 (не изменилось)
a.__dict__ # {'color': 'green'}
setattr:
setattr(Point, 'type_pt', 'disc')
Point.__dict__
getattr:
getattr(Point, 'color')
getattr(Point, 'nonexistent_attr', False) # возврат False если атрибут не найден
del:
del Point.type_pt
delattr:
delattr(Point, 'type_pt')
hasattr:
hasattr(Point, 'color') # True
del a.color
a.color # берется из класса Point
a.x = 1
a.y = 2
b.x = 10
b.y = 20
class Point:
"""Класс для представления точек на плоскости"""
...
Point.__doc__