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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Pipes in Python

14 点作者 sulami将近 7 年前

1 comment

joshuamorton将近 7 年前
I&#x27;ve mentioned this before, but both of these are overengineered. You need only define a custom class that overrides the | operator.<p>Then you get (from the original article)<p><pre><code> Pype(Users.find_all) | partial(group_by_category, max=5) | print </code></pre> where you have something like<p>class Pype: def __init__(self, f): self.f = f<p><pre><code> def __or__(self, f): return type(self)(f(self.f)) </code></pre> You need fancier tricks to be able to make the Pipe object transparent, but its possible.<p>The approach provided in this article is bad, it tries to reinvent a bunch of things (the operator module, for itemgetter, and stuff like getattr by wrapping them in strings.