TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

PEP 673 –– Self Type was accepted into Python

27 pointsby RojerGSover 3 years ago

3 comments

watersbover 3 years ago
I have yet to read the whole PEP, and I haven&#x27;t written any serious Python, either, but I find this to be very well written.<p>It uses a simple example of a &quot;Circle&quot; class, a subclass of &quot;Shape&quot;:<p><pre><code> class Shape: def set_scale(self, scale: float) -&gt; Shape: self.scale = scale return self Shape().set_scale(0.5) # =&gt; Shape class Circle(Shape): def set_radius(self, r: float) -&gt; Circle: self.radius = r return self Circle().set_scale(0.5) # *Shape*, not Circle Circle().set_scale(0.5).set_radius(2.7) # =&gt; Error: Shape has no attribute set_radius </code></pre> The error occurs because right now, Python cannot see that `self` is a `Circle`.<p>This PEP fixes that.
BiteCode_devabout 3 years ago
Python type hints are becoming easier to use every day. They started really terrible, but now with &quot;|&quot; and list&#x2F;set&#x2F;dict[] most common use cases are covered.<p>Now I would love to iter[] replace typing.Iterable[], callable[] replace typing.Callable[], and a shortcut for Annotated (maybe &amp; ?).
skissaneover 3 years ago
I’ve often wished Java had this exact feature (although I suppose in Java it would be called “this type” instead)