5 ms·
PEP 673 –– Self Type was accepted into Python
- watersb 5y agoI 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.
- skissane 5y agoI’ve often wished Java had this exact feature (although I suppose in Java it would be called “this type” instead)
- BiteCode_dev 5y agoPython type hints are becoming easier to use every day. They started really terrible, but now with "|" and list/set/dict[] most common use cases are covered. Now I would love to iter[] replace typing.Iterable[], callable[] replace typing.Callable[], and a shortcut for Annotated (maybe & ?).