TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

PEP 673 –– Self Type was accepted into Python

27 点作者 RojerGS超过 3 年前

3 条评论

watersb超过 3 年前
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_dev超过 3 年前
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; ?).
skissane超过 3 年前
I’ve often wished Java had this exact feature (although I suppose in Java it would be called “this type” instead)