PEP 673 –– Self Type was accepted into Python
python.org
python.org
It uses a simple example of a "Circle" class, a subclass of "Shape":
class Shape:
def set_scale(self, scale: float) -> Shape:
self.scale = scale
return self
Shape().set_scale(0.5) # => Shape
class Circle(Shape):
def set_radius(self, r: float) -> Circle:
self.radius = r
return self
Circle().set_scale(0.5) # *Shape*, not Circle
Circle().set_scale(0.5).set_radius(2.7)
# => Error: Shape has no attribute set_radius
The error occurs because right now, Python cannot see that `self` is a `Circle`.This PEP fixes that.
Now I would love to iter[] replace typing.Iterable[], callable[] replace typing.Callable[], and a shortcut for Annotated (maybe & ?).