Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions 02-descriptors.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
class TypeChecker:
required_type = object

def __init__(self, name):
def __set_name__(self, owner, name):
old_name = getattr(self, 'name', None)
if old_name:
raise ValueError(f'reused from {old_name}')

self.name = f'_{name}'

def __get__(self, instance, owner=None):
Expand All @@ -21,8 +25,8 @@ class IntType(TypeChecker):


class Point:
x = IntType('x')
y = IntType('y')
x = IntType()
y = IntType()

def __init__(self, x, y):
self.x = x
Expand All @@ -44,8 +48,8 @@ class PointType(TypeChecker):


class Circle:
center = PointType('center')
radius = IntType('radius')
center = PointType()
radius = IntType()

def __init__(self, center, radius):
self.center = center
Expand Down