3 ms·
I have yet to read the whole PEP, and I haven't written any serious Python, either, but I find this to be very well written. It uses a simple example of a "Cir
by watersb 5y ago
I have yet to read the whole PEP, and I haven't written any serious Python, either, but I find this to be very well written.
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.